PHP Control Structures and Loops

Themomsco [CPS] IN

In this tutorial, we will learn about PHP Control Structures & Loops. I will explain to you all the control structures used in a PHP script.

Any PHP script is built out of statements. You can understand a statement is like a functions call, loop, conditions or any code block that ends up with semicolon.

What is Control Structure in PHP

A control structure enables you to control the progress of code execution in your application. A script is executed consecutively, line by line, and a control structure enables you to change that flow depending on certain conditions. Other languages like Javascript, It’s a core feature of PHP language.

Let’s understand some control structures that PHP supports.

Some Type Of Control Structure in PHP

if else in PHP

It’s a very simple control structure and exactly the same as javascript and other languages.

Syntax The syntax is if… then… else.

See the code below:

if ($name == $username) {
  echo "Name and User Name are same";
}
else {
  print "They are not same";
}

In the above code, there are two code blocks one for if and another for else. If $name and $username value are same then the code executes of If block and print the given value.

But, if $name and $username value are not same then the code executes of else block and it displays the given value.

elseIf in PHP

“elseif” statement is a part of if… else… statement. It matches the second condition if the first condition does not match. There can be multiple elseif statements within an “ifelse” statement.

It extends an if statement to execute a different statement if the main if statement evaluates to FALSE. However, unlike else, it will execute the different statement if the alternative elseif conditional statement evaluates to TRUE.

see the reference code below:

if ($a > $b) {
    echo "a is bigger than b";
} elseif ($a == $b) {
    echo "a is equal to b";
} elseif (...) {
    echo "...";
} elseif (...) {
    echo "";
} else {
    echo "a is smaller than b";
}

In the above example, the output will be “a is bigger than b”, If the $a value greater than $b.  But If both values are same then the output will be “a is equal to b”.

Same way, you can write the multiple elseif statements to match a certain condition.

In the last, it goes into the else statement if no condition matches and output the value written there.

Switch in PHP

The switch statement is the same as the if…elseif…else… statement. A switch statement continues to display all the statements after finding one that is true, so breaks are used to prevent this.

In switch statements, each of the statement is followed by a break.

Syntax:

switch(condition){

  case value:
    //the code to be executed
  break;

  case value2:
    //the code to be executed
  break;

  case value3:
    //the code to be executed
  break;

  default:
    //default block code
  break;
}

Now, see the reference code below:

$today = "Monday";

switch($today){

  case "Tuesday":
    echo "Today is Tuesday.";
  break;

  case "Monday":
    echo "Today is Monday.";
  break;

  case "friday":
    echo "Today is Friday.";
  break;

  default:
    echo "Today is Holiday.";
  break;

}

In the above code, the value of $today is set to “Monday”.  So when the above code executes, the value of each case will be compared to $today value.

and finally the above code output the result “Today is Monday” because $today value matches with the case “Monday”.

Note: If you have a ton of elseif conditions in your page, then always try to use a Switch statement to make your code readable.

Also, Read: Calculate Array Length in PHP.

 

Themomsco [CPS] IN

Leave a Reply