PHP Unit 2
Conditional Statements
Conditional statements in PHP are used to perform different actions based on
different conditions.
They control the flow of execution in a script.
Types:
if Statement
if-else Statement
if-elseif-else Statement
Nested if Statement
switch Statement
Ternary Operator
if Statement
The if statement is used to execute a block of code only if a given condition
evaluates to true .
If the condition is true , the code inside the block runs; otherwise, it is skipped.
Syntax:
if (condition) {
// Code to execute if condition is true
}
- **Example:**
```php
$name = "Akira";
$age = 20;
if ($age >= 18) {
echo "$name is eligible to vote.";
}
if-else Statement
The if-else statement executes the if block of code if the condition is true,
otherwise, if the condition is false, the else block of code is executed
Syntax:
if (condition) {
// Code if condition is true
} else {
// Code if condition is false
}
Example:
$name = "Akira";
$score = 95;
if ($score >= 50) {
echo "$name has passed the test.";
} else {
echo "$name has failed the test.";
}
if-elseif-else Statement
The if-elseif-else statement checks multiple conditions sequentially and
executes the first matching block.
Helps handle multiple possible outcomes instead of just two.
If the first condition is false , it moves to the next elseif ; if none match, the else
block executes.
Syntax:
if (condition1) {
// Code if condition1 is true
} elseif (condition2) {
// Code if condition2 is true
} else {
// Code if all conditions are false
}
Example:
$name = "Akira";
$marks = 95;
if ($marks >= 90) {
echo "$name got an A grade!";
} elseif ($marks >= 75) {
echo "$name got a B grade!";
} else {
echo "$name got a C grade!";
}
Nested if Statement
A nested if statement is an if inside another if , used to check multiple
conditions within a hierarchy.
The inner if executes only if the outer if condition is true .
Syntax:
if (condition1) {
if (condition2) {
// Code if both conditions are true
}
}
Example:
$name = "Akira";
$age = 20;
$registered = true;
if ($age >= 18) {
if ($registered) {
echo "$name is eligible to vote.";
} else {
echo "$name needs to register to vote.";
}
} else {
echo "$name is not eligible to vote.";
}
switch Statement
The switch statement checks a variable against multiple values and executes the
corresponding block.
If a match is found, the corresponding block executes; break prevents fall-through
to the next case.
It is similar to if-elseif-else statement.
Syntax:
switch (variable) {
case value1:
// Code to execute if variable == value1
break;
case value2:
// Code to execute if variable == value2
break;
default:
// Code if no case matches
}
Example:
$name = "Akira";
$day = "Monday";
switch ($day) {
case "Monday":
echo "$name has a test today.";
break;
case "Wednesday":
echo "$name is planning to sleep.";
break;
default:
echo "$name has a regular day.";
}
Ternary operator
The ternary operator is a shorthand for if-else , returning one of two values based
on a condition.
Also known as conditional operator.
The first statement is executed if the condition is true, else the second statement is
executed.
Syntax:
variable = (condition) ? value_if_true : value_if_false;
Example:
$name = "Akira";
$score = 95;
$result = ($score >= 50) ? "$name passed the exam!" : "$name failed the
exam!";
echo $result;
Looping Statements
Looping statements are used to execute a block of code repeatedly based on a
condition.
Helps in automating repetitive tasks, reducing code duplication.
Types:
while Loop
do-while Loop
for Loop
foreach Loop
while Loop
The while loop executes code as long as the given condition remains true .
Used when the number of iterations is not predetermined.
It is an entry controlled loop, i.e. the condition is checked before executing the block.
Syntax:
while (condition) {
// Code to execute
}
Example:
$counter = 1;
while ($counter <= 4) {
echo "Akira is studying for exam $counter.<br>";
$counter++;
}
do-while Loop
Similar to while , but the block executes at least once, even if the condition is
false.
It is an exit controlled loop, i.e. the condition is checked after the block of code is
executed.
Syntax:
do {
// Code to execute
} while (condition);
Example:
$attempts = 1;
do {
echo "Attempt no.$attempts.<br>";
$attempts++;
} while ($attempts <= 3);
for Loop
The for loop runs a block of code a specific number of times.
It consists of three parts:
1. Initialization → Sets a starting value.
2. Condition → Loop continues while this is true .
3. Increment/Decrement → Updates the loop variable.
Syntax
for (initialization; condition; increment/decrement) {
// Code to execute
}
Example:
for ($i = 1; $i <= 5; $i++) {
echo "Task no.$i.<br>";
}
foreach Loop
The foreach loop is used to iterate over arrays.
It automatically assigns each array element to a variable.
Used to access each individual elements of an array.
Syntax:
foreach ($array as $value) {
// Code to execute
}
Example:
$names = ["Akira", "Ayaka", "Ayumi"];
foreach ($names as $n) {
echo "Student name is: $n.<br>";
}
Jumping Statements
Jumping statements are used to control the flow of execution by skipping or
stopping iterations inside loops and conditional structures.
Types:
break Statement
continue Statement
break Statement
Immediately exits a loop or switch statement.
Used when a specific condition is met, and further iterations are unnecessary.
Example:
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
echo "Stopping at $i<br>";
break;
}
echo "Step $i completed.<br>";
}
continue Statement
Skips the current iteration without exiting the loop.
The loop continues with the next iteration.
Example:
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
echo "Skipping step $i...<br>";
continue;
}
echo "Step $i completed.<br>";
}
Akira (R)