Variables: ?PHP $name "Amit" $age 19 Echo "Name: $name, Age: $age" ?
Variables: ?PHP $name "Amit" $age 19 Echo "Name: $name, Age: $age" ?
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"];
Functions in PHP
<?php
//Basic Function
function sayHello() {
echo "Hello from function!";
}
sayHello();
greet("Amit");
php 2
function add($a, $b) {
return $a + $b;
}
?>
<?php
$marks = 85;
php 3
<?php
$marks = 85;
php 4
// Multidimensional array (array inside array)
$students = [
["Rahul", 20, "A"],
["Anjali", 19, "B"]
];
php 5
<?php
class Car {
public $brand;
function __construct($brand) {
$this->brand = $brand;
}
function showBrand() {
return "This car is a " . $this->brand;
}
}
<?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>
<?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.
$val = "0";
echo isset($val); // true
empty()
1. Checks if the variable is empty, including:
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>
<?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>
<?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";
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully<br>";
if (mysqli_query($conn, $sqlCreate)) {
echo "Table users created successfully<br>";
} else {
echo "Error creating table: " . mysqli_error($conn) . "<br>";
}
if (mysqli_query($conn, $sqlInsert)) {
echo "New record inserted successfully<br>";
} else {
echo "Error inserting record: " . mysqli_error($conn) . "<br>";
}
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