php
Variables
<?php
$name = "Amit";
$age = 19;
echo "Name: $name, Age: $age";
?>
Loops
<?php
// For loop
for ($i = 1; $i <= 5; $i++) {
echo "Number: $i <br>";
}
// While loop
$count = 1;
while ($count <= 3) {
echo "Count: $count <br>";
$count++;
}
php 1
// Foreach Loop (used with arrays)
$fruits = ["Apple", "Banana", "Mango"];
foreach ($fruits as $fruit) {
echo "I like $fruit <br>";
}
?>
Functions in PHP
<?php
//Basic Function
function sayHello() {
echo "Hello from function!";
}
sayHello();
//func with parameters
function greet($name) {
echo "Hello, $name!";
}
greet("Amit");
//func with return value
php 2
function add($a, $b) {
return $a + $b;
}
$sum = add(5, 10);
echo "Sum is: $sum";
?>
<?php
$marks = 85;
if ($marks >= 90) {
echo "Grade: A+";
}
elseif ($marks >= 80) {
echo "Grade: A";
}
elseif ($marks >= 70) {
echo "Grade: B";
}
else {
echo "Grade: C or below";
}
?>
php 3
<?php
$marks = 85;
if ($marks >= 90) {
echo "Grade: A+";
} elseif ($marks >= 80) {
echo "Grade: A";
} elseif ($marks >= 70) {
echo "Grade: B";
} else {
echo "Grade: C or below";
}
?>
&& // AND → दोनों शर्तें सही होनी चाहिए
|| // OR → कोई एक शर्त सही होनी चाहिए
! // NOT → उल्टा कर देता है (true → false)
and // AND (low priority)
or // OR (low priority)
xor // XOR → सिर्फ एक ही शर्त सही होनी चाहिए
// Indexed array (ordered values)
$fruits = ["apple", "banana", "mango"];
// Associative array (key-value pairs)
$marks = ["Math" => 90, "English" => 85];
php 4
// Multidimensional array (array inside array)
$students = [
["Rahul", 20, "A"],
["Anjali", 19, "B"]
];
count($fruits); // Get number of elements
array_push($fruits, "grape"); // Add element at the end
array_pop($fruits); // Remove last element
array_keys($marks); // Get all keys
array_values($marks); // Get all values
$fruits = ["apple", "banana", "mango"];
echo $fruits[0]; // Output: apple
echo $fruits[1]; // Output: banana
echo $fruits[2]; // Output: mango
foreach ($marks as $subject => $score) {
echo "$subject = $score <br>"; // Print each subject and
}
Object-Oriented PHP (OOP)
php 5
<?php
class Car {
public $brand;
function __construct($brand) {
$this->brand = $brand;
}
function showBrand() {
return "This car is a " . $this->brand;
}
}
$myCar = new Car("BMW");
echo $myCar->showBrand();
?>
Connecting PHP to MySQL Database
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test_db";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully!";
?>
php 6
html with php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="index.php" method="post">
<label>Name:</label>
<input type="text" name="username" ><br>
<label>Password:</label>
<input type="password" name="password"><br>
<input type="submit" value="login">
</form>
</body>
</html>
<?php
echo $_POST['username'] . "<br>";
echo $_POST['password'] . "<br>";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
php 7
<form action="index.php" method="post">
<label>Name:</label>
<input type="text" name="quantity" ><br>
<input type="submit" value="submit">
</form>
</body>
</html>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$quantity = $_POST['quantity'];
echo "You entered: {$quantity}";
echo "You entered: $quantity"; // or
}
?>
isset()
1. Checks if variable is set and not null.
2. Returns true even if the value is 0 , "0" , or ""
$val = "0";
echo isset($val); // true
empty()
1. Checks if the variable is empty, including:
"" , 0 , "0" , null , false , [] , not set
2. Returns true if the value looks like "nothing" (even "0" is treated empty).
php
CopyEdit
php 8
$val = "0";
echo empty($val); // true
Radio button
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Select Theme</title>
</head>
<body>
<form method="post" action="index.php">
<h3>Select Your Theme:</h3>
<label><input type="radio" name="theme" value="Dark"> Dark</label><
<label><input type="radio" name="theme" value="Light"> Light</label><
<input type="submit" name="submit" value="Submit">
</form>
<?php
if (isset($_POST['submit'])) {
if (isset($_POST['theme']) && !empty($_POST['theme'])) {
echo "You selected the " . $_POST['theme'] . " theme.";
} else {
echo "Please select a theme.";
}
}
?>
</body>
</html>
Checkbox
php 9
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Checkbox Example</title>
</head>
<body>
<form method="post" action="index.php">
<h3>Select your hobbies:</h3>
<label><input type="checkbox" name="hobbies[]" value="Badminton">
<label><input type="checkbox" name="hobbies[]" value="Table Tennis"
<label><input type="checkbox" name="hobbies[]" value="Traveling"> T
<input type="submit" name="submit" value="Submit">
</form>
<?php
if (isset($_POST['submit'])) {
if (!empty($_POST['hobbies'])) {
echo "You selected these hobbies:<br>";
// Loop through all selected hobbies
foreach ($_POST['hobbies'] as $hobby) {
echo htmlspecialchars($hobby) . "<br>";
}
} else {
echo "Please select at least one hobby.";
}
}
?>
</body>
</html>
php 10
MySQL operations with php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "testdb";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully<br>";
$sqlCreate = "CREATE TABLE IF NOT EXISTS users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRE
)";
if (mysqli_query($conn, $sqlCreate)) {
echo "Table users created successfully<br>";
} else {
echo "Error creating table: " . mysqli_error($conn) . "<br>";
}
$sqlInsert = "INSERT INTO users (username, email) VALUES ('JohnDoe', 'john@
if (mysqli_query($conn, $sqlInsert)) {
echo "New record inserted successfully<br>";
} else {
echo "Error inserting record: " . mysqli_error($conn) . "<br>";
}
$sqlSelect = "SELECT id, username, email, reg_date FROM users";
$result = mysqli_query($conn, $sqlSelect);
php 11
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo "ID: " . $row["id"] .
" | Username: " . $row["username"] .
" | Email: " . $row["email"] .
" | Registered: " . $row["reg_date"] . "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
php 12