Control Structures in PHP

Every programming language includes some syntax and structure to control the flow and execution of a program.PHP includes some control structure such as loop (for, while) and conditions (If, else).

PHP Condition Statement

The if Statement

The if is the most important feature in PHP including all the programing language.It executes the code when one of its condition is true.

Syntax :-  

<?php
if (condition) {
    statement if condition is true;
}
?>

Example :-

<?php
$color = "White";
if ($color == "White") {
    echo "It is a symbol of peace";
}
?>

Output
It is a symbol of peace

Example :-

<?php
$Number =  22;
if ($Number % 2==0 ) {
    echo "It is a even number";
}
?>

Output
It is a even number

The if...else Statement

The else condition is slightly different from the if condition it execute the code when one condition is true and another condition is false and we have to check both the conditions in order to get the correct result.

Syntax :-  

<?php
if (condition) {
    statement if condition is true;
} else {
    statement if condition is false;
}
?>

Example :- 

<?php
$status = 1;
if ($status = 1) {
    echo "User is Active!";
} else {
    echo "User is Inactive!";
}
?>

Output
User is active!

Example :- 

<?php
$Total_student = 89;
if ($Total_student < 49) {
    echo "Good class Strength!";
} else {
    echo "Needs Improvement!";
}
?>

Output
Needs Improvement!

The if... elseif....else Statement

The if... elseif....else statement is executed where there are more than two conditions.If the first condition is not fulfilled it jump to the next condition and if both conditions was not correct then it will print default result. 

Syntax :-  

<?php
if (condition) {
    statement if condition is true;
} elseif (condition) {
    statement if condition is true;
} else {
     statement if condition are false;
}
?>

Example :- 

<?php
$number = 100;
if ($number > 0) {
    echo "Your number is greater then 10";
} elseif ($number < 0) {
    echo "Your number is Less then 10";
} else {
    echo "Your number is 0";
}
?>

Output
your number is greater then 10

Example :- 

<?php
$day = "Saturday";
if ($day == "Monday") {
    echo "Today is First day of the week";
} elseif ($day == "Sunday") {
    echo "Today is last day of the week";
} else {
    echo "Your are in the middle of the week";
}
?>

Output
Your are in the middle of the week

The switch Statement

The switch statement works similar as if... else statement instead of  giving if.... else condition, we use the switch statement to achieve different action based on their condition.

<?php
switch (n) {
    case label 1:
        code if n=label 1;
        break;
    case label 2:
        code if n=label 2;
        break;
    case label 3:
        code if n=label 3;
        break;
    ...
    default:
        code if n is different from all labels;
}
?>

Example :- 

<?php
$flag = "Ashoka Chakra";
switch ($flag) {
    case "Ashoka Chakra":
        echo "This is pride of India";
        break;
    case " crescent moon ":
        echo "This is pride of pakistan ";
        break;
    case "white stars":
        echo "This is pride of United States of America";
        break;
    default:
        echo "Not exist in the list";
}
?>

Output
This is pride of India

Example :-

<?php
$Population = 1342512706;
switch($Population)
{
	case  1342512706:
        echo "India population in 2018 is $Population";
    break;
	case 1342512706:
        echo "India population in 2017  is $Population";
    break;
    case  1326801576: 
        echo "India population in 2016  is $Population";
    break;
    case  1311050527: 
        echo  "India population in 2015  is $Population";
    break;
    default: 
        echo 'No data avalible';
    break;
}
?>

Output
India population in 2018 is 1342512706

 

 


Keywords: