0% found this document useful (0 votes)
8 views

4. PHP Decision Making and Loops

Uploaded by

imsisia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

4. PHP Decision Making and Loops

Uploaded by

imsisia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

PHP DECISION MAKING AND LOOPS

In programming, decision-making structures allow you to control the flow of your code based on
certain conditions. Loops, on the other hand, enable repetitive tasks efficiently by iterating over
data or repeating actions until a condition is met. Below are detailed notes on both concepts, with
examples and real-world applications.

Decision Making in PHP

1. if, else, elseif Statements

Definition

• if statement -- Executes a block of code only if a specified condition is true.


• else statement -- Provides an alternative block of code to execute if the if condition is
false.
• elseif statement -- Adds more conditions to check after the if statement.

Syntax

if (condition) {
// Code to execute if condition is true
} elseif (another_condition) {
// Code to execute if the second condition is true
} else {
// Code to execute if all conditions are false
}

Example

$age = 20;

if ($age < 13) {


echo "You are a child.";
} elseif ($age >= 13 && $age < 20) {
echo "You are a teenager.";
} else {
echo "You are an adult.";
}

Real-World Application

• Login Systems -- Check if a user’s input matches stored credentials.


• Age-based Restrictions -- Verify if someone is eligible for voting, driving, or accessing
age-restricted content.
2. Switch Statements

Definition

The switch statement is an alternative to using multiple if conditions. It evaluates an expression


and executes code based on matching cases.

Syntax

switch (expression) {
case value1 --
// Code to execute if expression == value1
break;
case value2 --
// Code to execute if expression == value2
break;
default --
// Code to execute if no case matches
}

Example

$day = "Monday";

switch ($day) {
case "Monday" --
echo "Start of the work week!";
break;
case "Friday" --
echo "Weekend is near!";
break;
default --
echo "Just another day.";
}

Real-World Application

• Menu Selection -- Display different actions based on the user's choice in a menu.
• Routing -- Handle different actions in a web application based on a user’s URL or input.

Loops in PHP

1. For Loop

Definition
The for loop is used when the number of iterations is known in advance. It consists of three
parts -- initialization, condition, and increment/decrement.

Syntax

for (initialization; condition; increment) {


// Code to execute in each iteration
}

Example

for ($i = 1; $i <= 5; $i++) {


echo "Iteration -- $i<br>";
}

Real-World Application

• Generating Reports -- Print a list of items, such as sales data, in a table.


• Pagination -- Display a fixed number of items per page.

2. While Loop

Definition

The while loop executes a block of code as long as the specified condition is true. It’s useful
when the number of iterations is not predetermined.

Syntax

while (condition) {
// Code to execute
}

Example

$count = 1;

while ($count <= 5) {


echo "Count is -- $count<br>";
$count++;
}

Real-World Application

• User Input Validation -- Keep asking for correct input until valid data is provided.
• Downloading Files -- Check for download progress in a loop until complete.
3. Do...While Loop

Definition

The do...while loop is similar to the while loop but guarantees that the code block executes at
least once before the condition is checked.

Syntax

do {
// Code to execute
} while (condition);

Example

$count = 1;

do {
echo "Count is -- $count<br>";
$count++;
} while ($count <= 5);

Real-World Application

• User Login Attempts -- Ensure that a user is prompted for a password at least once.
• Game Scenarios -- Ensure an action like rolling dice occurs at least once.

4. Foreach Loop

Definition

The foreach loop is used to iterate over arrays, making it the best choice for handling
collections of data.

Syntax

foreach ($array as $value) {


// Code to execute for each element
}

foreach ($array as $key => $value) {


// Code to execute using key-value pairs
}

Example
$fruits = ["Apple", "Banana", "Orange"];

foreach ($fruits as $fruit) {


echo $fruit . "<br>";
}

Real-World Application

• Shopping Carts -- Display each item and its price in an online cart.
• Processing Form Data -- Loop through form inputs to save them into a database.

Summary Table of Use Cases

Control Structure Use Case


if, else, elseif Check login credentials or validate input
switch Select menu options or route to pages
for loop Generate tables, lists, or pagination numbers
while loop Repeat actions with uncertain iterations (e.g., downloads)
do...while loop Actions that must occur at least once (e.g., retries)
foreach loop Display all items in a collection like product lists

You might also like