FahmidasClassroom

Learn by easy steps

Learning Objectives

After completing this tutorial, you will be able to:

  • Understand the importance of decision-making in programming.
  • Use conditional statements to control program flow.
  • Apply nested and multiple conditions.
  • Use different types of loops to perform repetitive tasks.
  • Select the appropriate loop for different programming problems.
  • Build small PHP applications using conditions and loops.
  • Use AI tools to debug and improve PHP programs.

Introduction

Programs often need to make decisions and repeat tasks. For example, a student grading system determines whether a student has passed or failed based on marks. An ATM checks whether the account has sufficient balance before allowing a withdrawal. A payroll system calculates salaries for hundreds of employees using repetitive operations.

Conditional statements allow a program to make decisions, while loops allow it to repeat a block of code efficiently. Together, these concepts form the foundation of programming logic and are used in nearly every PHP application.

Prerequisites

Before starting this tutorial, you should be familiar with:

  • PHP variables
  • Data types
  • Operators
  • Input and output

Conditional Statements

Conditional statements execute different blocks of code depending on whether a condition is true or false.

PHP provides the following conditional statements:

  • if
  • if…else
  • if…elseif…else
  • switch
  • match

The if Statement

The if statement executes a block of code only when the specified condition is true.

Syntax

if (condition) {

// code

}

Example-1

<?php

$age = 20;

if ($age >= 18) {

    echo "You are eligible to vote.";

}

?>

 

Output:

The if…else Statement

When the condition is false, the else block executes.

Example-2

<?php

$marks = 45;

if ($marks >= 50) {
     echo "Pass";
}
else {
     echo "Fail";
}

?>

Output:

The if…elseif…else Statement

This statement checks multiple conditions.

Example-3

<?php

$marks = 82;
if ($marks >= 80) {
    echo "Grade A";
}
elseif ($marks >= 70) {
    echo "Grade B";
}
elseif ($marks >= 60) {
     echo "Grade C";
}
elseif ($marks >= 50) {
     echo "Grade D";
}
else {
     echo "Fail";
}

?>

 

Output:

Nested if Statement

A nested if statement places one if statement inside another.

Example-4

<?php

$age = 22;
$citizen = true;
if ($age >= 18) {
    if ($citizen) {
         echo "Eligible to vote";
    }
}

?>

Output:

The switch Statement

The switch statement is useful when comparing a variable against multiple fixed values.

Syntax:

switch ($variable) {

case value1:

break;

case value2:

break;

default:

}

Example-5

<?php

$day = 3;
switch ($day) {

case 1:
   echo "Sunday";
   break;

case 2:
   echo "Monday";
   break;

case 3:
   echo "Tuesday";
   break;

case 4:
   echo "Wednesday";
   break;

default:
   echo "Invalid Day";

}

?>

 

Output:

The match Expression

PHP 8 introduced the match expression, which provides a cleaner alternative to switch.

Example-6

<?php

$grade = "A";

$result = match($grade) {
     "A" => "Excellent",
     "B" => "Very Good",
     "C" => "Good",
     "D" => "Average",
     default => "Fail"
};

echo $result;

?>

Output:

Advantages of match

• Cleaner syntax

• No break statement

• Strict comparison

• Returns a value

Loops

Loops execute the same block of code multiple times.

PHP provides four major loops.

• while

• do…while

• for

• foreach

The while Loop

The while loop executes as long as the condition remains true.

Syntax

while(condition){

}

Example-7

<?php

$i = 1;

while($i <= 5){
    echo $i . "<br>";
    $i++;
}

?>

Output:

The do…while Loop

The do…while loop executes at least once before checking the condition.

Example-8

<?php

i = 1;
do{
    echo $i . "<br>";
    $i++;
}
while($i <= 5);

?>

Output:

The for Loop

The for loop is ideal when the number of repetitions is known.

Syntax

for(initialization; condition; increment){

}

Example-9

<?php

for($i = 1; $i <= 10; $i++){
     echo $i . "<br>";
}

?>

Output:

Multiplication Table

Example-10

<?php

$number = 7;
for($i=1;$i<=10;$i++){
      echo $number . " x " . $i . " = " . ($number*$i);
      echo "<br>";
}

?>

Output:

The foreach Loop

The foreach loop is designed for arrays.

Example-11

<?php

$subjects = [“PHP”,”HTML”,”CSS”,”JavaScript”];

foreach($subjects as $subject){

echo $subject;

echo “<br>”;

}

?>

Output:

Break Statement

The break statement immediately terminates a loop.

Example-12

<?php

for($i=1;$i<=10;$i++){
    if($i==6){
          break;
    }
    echo $i."<br>";
}

?>

Output:

Continue Statement

The continue statement skips the current iteration.

Example-13

<?php

for($i=1;$i<=5;$i++){
    if($i==3){
          continue;
    }
    echo $i."<br>";
}

?>

Output:

Example 1

Determine Even or Odd

<?php

$number = 18;
if($number % 2 == 0){
    echo "Even Number";
}
else{
     echo "Odd Number";
}

?>

Example 2

Largest of Three Numbers

<?php

$a = 25;
$b = 30;
$c = 18;

if($a >= $b && $a >= $c){
     echo $a;
}

elseif($b >= $a && $b >= $c){
     echo $b;
}

else{
     echo $c;
}

?>

Example 3

Print Even Numbers

<?php

for($i=2;$i<=20;$i+=2){
     echo $i."<br>";
}

?>

Example 4

Sum of Numbers from 1 to 100

<?php

$sum = 0;

for($i=1;$i<=100;$i++){
    $sum += $i;
}

echo $sum;

?>

Example 5

Display the Alphabet

<?php

for($ch='A'; $ch<='Z'; $ch++){
     echo $ch . " ";
}

?>

Lab works:

Task 1:

Write a PHP program to determine whether a number is positive, negative, or zero.

Task 2:

Write a program to determine whether a student has passed or failed.

Task 3:

Print numbers from 1 to 50 using a while loop.

Task 4:

Print the multiplication table of any number.

Task 5:

Display all even numbers from 1 to 100.

Try Yourself:

  • Determine whether a given year is a leap year.
  • Check whether a number is divisible by both 3 and 5.
  • Print all odd numbers between 1 and 100.
  • Calculate the sum of all even numbers between 1 and 200.
  • Print the multiplication tables from 1 to 10.
  • Reverse the digits of an integer.
  • Count the number of digits in an integer.
  • Check whether a number is an Armstrong number.
  • Check whether a number is a palindrome.
  • Build a simple menu-driven calculator using switch or match that performs addition, subtraction, multiplication, and division based on the user’s choice.