Programming with PHP
Conditional Statements in PHP
Conditional statements are used to perform different actions based on
different conditions.
1. if Statement
The if statement executes a block of code only if the given condition
evaluates to true.
Syntax:
if (condition) {
// Code to be executed if the condition is true
}
Example:
$age = 20;
if ($age >= 18) {
echo "You are eligible to vote.";
}
2. if-else Statement
The if-else statement executes one block of code if the condition is
true, otherwise, it executes another block.
Syntax:
if (condition) {
// Code to be executed if the condition is true
} else {
// Code to be executed if the condition is false
}
Example:
$marks = 45;
if ($marks >= 50) {
echo "You have passed.";
} else {
echo "You have failed.";
}
3. if-elseif-else Statement
This allows checking multiple conditions.
Syntax:
if (condition1) {
// Code if condition1 is true
} elseif (condition2) {
// Code if condition2 is true
} else {
// Code if all conditions are false
}
Example:
$score = 75;
if ($score >= 90) {
echo "Grade: A";
} elseif ($score >= 75) {
echo "Grade: B";
} else {
echo "Grade: C";
}
4. switch Statement
The switch statement is used when there are multiple possible values
for a single variable.
Syntax:
switch (variable) {
case value1:
// Code to execute
break;
case value2:
// Code to execute
break;
default:
// Code to execute if none of the cases match
}
Example:
$day = "Monday";
switch ($day) {
case "Monday":
echo "Start of the week.";
break;
case "Friday":
echo "Weekend is coming!";
break;
default:
echo "It's a regular day.";
}
5. The Ternary Operator (? :)
The ternary operator is a shorthand way of writing an if-else
statement.
Syntax:
condition ? expression_if_true : expression_if_false;
Example:
$age = 18;
$result = ($age >= 18) ? "Eligible to vote" : "Not eligible";
echo $result;
Looping Statements in PHP
Loops are used to execute a block of code multiple times.
1. while Loop
Executes a block of code as long as the specified condition is true.
Syntax:
while (condition) {
// Code to be executed
}
Example:
$x = 1;
while ($x <= 5) {
echo "Number: $x <br>";
$x++;
}
2. do-while Loop
Similar to while, but guarantees at least one execution before
checking the condition.
Syntax:
do {
// Code to be executed
} while (condition);
Example:
$x = 1;
do {
echo "Number: $x <br>";
$x++;
} while ($x <= 5);
3. for Loop
Used when the number of iterations is known.
Syntax:
for (initialization; condition; increment/decrement) {
// Code to be executed
}
Example:
for ($i = 1; $i <= 5; $i++) {
echo "Number: $i <br>";
}
Arrays in PHP
An array is a data structure that holds multiple values in a single
variable.
1. Creating Arrays
Arrays can be created using the array() function or square brackets
[].
Example:
$fruits = array("Apple", "Banana", "Cherry");
$numbers = [10, 20, 30];
2. Accessing Array Elements
Elements in an array are accessed using their index.
Example:
$fruits = ["Apple", "Banana", "Cherry"];
echo $fruits[1]; // Output: Banana
3. Types of Arrays
a) Indexed Arrays
Uses numeric indexes.
$colors = ["Red", "Green", "Blue"];
echo $colors[0]; // Output: Red
b) Associative Arrays
Uses named keys.
$student = ["name" => "John", "age" => 25];
echo $student["name"]; // Output: John
c) Multidimensional Arrays
Contains arrays within arrays.
$students = [
["John", 25, "A"],
["Jane", 22, "B"]
];
echo $students[1][0]; // Output: Jane
4. Manipulating Arrays
Adding Elements
$fruits[] = "Mango";
Removing Elements
unset($fruits[1]);
5. Displaying Arrays
Using print_r() or var_dump()
$fruits = ["Apple", "Banana"];
print_r($fruits);
6. Useful Array Functions
count($array):Counts elements in an array.
array_push($array, $value): Adds elements to the end.
array_pop($array): Removes last element.
array_shift($array): Removes first element.
array_unshift($array, $value): Adds elements to the
beginning.
Including and Requiring Files in PHP
PHP allows including external files using include() and require().
1. include()
Includes a file, but does not stop execution if the file is missing.
include '[Link]';
2. require()
Includes a file but causes a fatal error if the file is missing.
require '[Link]';
Implicit and Explicit Casting in PHP
1. Implicit Casting
PHP automatically converts data types when required.
Example:
$x = "10" + 5; // $x is now 15 (integer)
2. Explicit Casting
Manually converting data types.
Example:
$number = (int) "123"; // Converts string to integer