Unit 2
Control Structures and Array in PHP
CONTROL STRUCTURES IN PHP:
Control structures allow the program to make decisions and repeat tasks based on
conditions.
Common control structures in PHP include:
1. if statement
2. if...else statement
3. if...elseif...else statement
4. switch statement
5. Loops
These structures control the flow of execution in a program.
If Statement
The if statement executes a block of code only when the specified condition is true.
Syntax
if (condition) {
// code to be executed
}
Example
<?php
$age = 18;
if ($age >= 18) {
echo "You are eligible to vote.";
}
?>
Output: You are eligible to vote.
Explanation: If the condition $age >= 18 is true, the message is displayed.
If...Else Statement
The if...else statement executes one block of code if the condition is true and another
block if it is false.
Syntax
if (condition) {
// code if condition is true
}
else {
// code if condition is false
}
Tunga Mahavidyalaya
Example
<?php
$number = 10;
if ($number % 2 == 0) {
echo "Even Number";
}
else {
echo "Odd Number";
}
?>
Output:
Even Number
If...Elseif...Else Statement
Used when multiple conditions need to be checked.
Syntax
if (condition1) {
// code
}
elseif (condition2) {
// code
}
else {
// code
}
Example
<?php
$marks = 75;
if ($marks >= 90) {
echo "Grade A";
}
elseif ($marks >= 60) {
echo "Grade B";
}
else {
echo "Grade C";
}
?>
Output: Grade B
Tunga Mahavidyalaya
Switch Statement
The switch statement is used to execute different actions based on different conditions.
It is often used instead of multiple if...elseif statements.
Syntax
switch (variable) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code
}
Example
<?php
$day = 3;
switch ($day) {
case 1:
echo "Monday";
break;
case 2:
echo "Tuesday";
break;
case 3:
echo "Wednesday";
break;
default:
echo "Invalid day";
}
?>
Output: Wednesday
Tunga Mahavidyalaya
Loops in PHP
Loops are used to execute a block of code repeatedly as long as a specified condition is
true.
Types of Loops in PHP
1. while loop
2. do...while loop
3. for loop
4. foreach loop
Loops reduce code repetition and make programs efficient.
While Loop
The while loop executes a block of code repeatedly as long as the condition is true.
Syntax
while (condition) {
// code to execute
}
Example
<?php
$i = 1;
while ($i <= 5) {
echo $i;
$i++;
}
?>
Output
12345
Explanation:
• The loop starts with $i = 1.
• It runs while $i <= 5.
• $i++ increases the value each time.
Do...While Loop
The do...while loop executes the block of code at least once, even if the condition is false.
Syntax
do {
// code
} while (condition);
Example
<?php
$i = 1;
do {
echo $i;
$i++;
} while ($i <= 5);
?>
Tunga Mahavidyalaya
Output
12345
For Loop
The for loop is used when the number of iterations is known.
Syntax
for (initialization; condition; increment) {
// code
}
Example
<?php
for ($i = 1; $i <= 5; $i++) {
echo $i;
}
?>
Output
12345
Explanation:
• Initialization: $i = 1
• Condition: $i <= 5
• Increment: $i++
Foreach Loop
The foreach loop is used to iterate through arrays.
Syntax
foreach ($array as $value) {
// code
}
Example
<?php
$fruits = array("Apple", "Banana", "Orange");
foreach ($fruits as $fruit) {
echo $fruit . "<br>";
}
?>
Output
Apple
Banana
Orange
Explanation:
• $fruits is the array.
• $fruit represents each element in the array during iteration.
Tunga Mahavidyalaya
Break Statement in PHP
The break statement is used to terminate the execution of a loop or a switch statement
immediately.
When break is encountered, the control exits the loop or switch block.
Example – Break in a Loop
<?php
for ($i = 1; $i <= 10; $i++) {
if ($i == 5) {
break;
}
echo $i;
}
?>
Output
1234
Explanation:
• The loop starts from 1.
• When $i becomes 5, the break statement stops the loop.
Continue Statement
The continue statement is used to skip the current iteration of the loop and move to the
next iteration.
Example
<?php
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
continue;
}
echo $i;
}
?>
Output
1245
Explanation:
• When $i = 3, the loop skips printing 3.
• Execution continues with the next iteration.
Nested Loops
A nested loop is a loop inside another loop.
Nested loops are commonly used for:
• pattern printing
• multidimensional arrays
• matrix operations
Tunga Mahavidyalaya
Example
<?php
for ($i = 1; $i <= 3; $i++) {
for ($j = 1; $j <= 3; $j++) {
echo "$i$j ";
}
echo "<br>";
}
?>
Output
11 12 13
21 22 23
31 32 33
Explanation:
• The outer loop runs three times.
• The inner loop runs three times for each outer loop iteration.
PHP Program Example
Example program demonstrating loops and conditions:
<?php
$numbers = array(10, 20, 30, 40, 50);
foreach ($numbers as $num) {
if ($num == 30) {
continue;
}
echo $num . "<br>";
}
?>
Output
10
20
40
50
Explanation:
• The array contains five numbers.
• When the value 30 appears, it is skipped using continue.
Tunga Mahavidyalaya