Learn coding with amol
Hello my name is amol sharma or vineet and this is my 10th part of learn coding with amol and in this part we are going to learn php.
Php PART -10
Complete PHP Programming Guide
PHP (Hypertext Preprocessor) is a powerful server-side scripting language used mainly for web development. This guide takes you from beginner to advanced with examples and explanations.
1. Introduction to PHP
PHP code runs on the server and generates HTML that is sent to the client’s browser.
<?php echo "Hello, World!"; ?>
2. PHP Syntax
- PHP code starts with
<?phpand ends with?> - Statements end with
; - Comments:
// single line,/* multi-line */
<?php // Single-line comment # Another single-line comment /* Multi-line comment */ echo "Syntax Example!"; ?>
3. Variables and Data Types
- Variables start with
$ - Types: string, integer, float, boolean, array, object
<?php $name = "Amol"; $age = 21; $price = 99.99; $is_admin = true; echo "Name: $name, Age: $age, Price: $price, Admin: $is_admin"; ?>
4. Operators
- Arithmetic:
+,-,*,/,% - Comparison:
==,!=,<,>,<=,>= - Logical:
&&,||,!
<?php $a = 10; $b = 5; echo $a + $b; // 15 echo $a > $b; // true ?>
5. Conditional Statements
<?php
$age = 18;
if($age >= 18) {
echo "You are an adult";
} else {
echo "You are a minor";
}
?>
6. Loops
<?php
for($i=1; $i<=5; $i++) {
echo "Number: $i <br>";
}
$colors = ["red","green","blue"];
foreach($colors as $c) {
echo $c . "<br>";
}
?>
7. Functions
<?php
function greet($name) {
return "Hello, $name!";
}
echo greet("Amol");
?>
8. Arrays
<?php $fruits = ["Apple","Banana","Mango"]; echo $fruits[0]; // Apple $person = ["name"=>"Amol", "age"=>21]; echo $person["name"]; // Amol ?>
9. Form Handling
Create a form and handle data with PHP:
<form method="post">
Name: <input type="text" name="username">
<input type="submit">
</form>
<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
$user = $_POST["username"];
echo "Hello, $user";
}
?>
10. File Handling
<?php
$file = fopen("test.txt","w");
fwrite($file,"Hello File!");
fclose($file);
$read = file_get_contents("test.txt");
echo $read;
?>
11. Object-Oriented PHP
<?php
class Person {
public $name;
function __construct($n) {
$this->name = $n;
}
function greet() {
return "Hello, " . $this->name;
}
}
$p = new Person("Amol");
echo $p->greet();
?>
12. Database Connection (MySQLi)
<?php
$conn = new mysqli("localhost","root","","testdb");
if($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected Successfully!";
$conn->close();
?>
13. Advanced Topics
- Sessions and Cookies
- PDO (PHP Data Objects)
- Error Handling with
try/catch - Include & Require statements
- PHP Security (XSS, SQL Injection prevention)
14. Practice Program
<?php
// Program to calculate factorial
function factorial($n) {
if($n == 0) return 1;
return $n * factorial($n-1);
}
echo factorial(5); // Output: 120
?>
🎯 With this complete PHP guide, you can start building dynamic websites, handle forms, connect to databases, and even make full web applications!

Comments
Post a Comment