Thursday, May 7, 2020

Conditional Control Structures

Conditional statements are used to perform different actions for different conditions. In PHP we have the following conditional statements:

  • if statement – executes some code if one condition is true
  • if…else statement – executes some code if a condition is true and another code if that condition is false
  • if…elseif…else statement – executes different codes for more than two conditions
  • switch statement – selects one of many blocks of code to be executed

The if Statement

The if statement executes some code if one condition is true.

<?php
$t = date("H");
// if structure
if ($t < "15")
{
echo "Have a good day!";
}
?> 

The if…else Statement

The if…else statement executes some code if a condition is true and another code if that condition is false.

<?php
$t = date("H");
// if-else control Structuer
    if ($t < "15")
    {
    echo "Have a good day!";
    }
    else
    {
    echo "Have a good night!";
    }
?>

The if…elseif…else Statement

The if…elseif…else statement executes different codes for more than two conditions.

<?php
$t = date("H");
// if-esleif-else control structure
    if ($t < "15")
    {
    echo "Have a good morning!";
    }
    elseif ($t < "25")
    {
    echo "Have a good day!";
    }
    else
    {
    echo "Have a good night!";
    }
?>

The Switch Statement

The switch statement is used to perform different actions based on different conditions. It is used to select one of many blocks of code to be executed.

<?php
$favcolor = "blue";
// Switch control Structure
    switch ($favcolor)
    {
    case "blue":
    echo "Your favorite color is blue!";
    break;
    case "green":
    echo "Your favorite color is green!";
    break;
    case "black":
    echo "Your favorite color is black!";
    break;
    default:
    echo "Your favorite color is neither blue, green nor black!";
}

?> 

PHP Loops

When we write a code, we want the same block of code to run over and over again in a row. Instead of adding several almost equal code-lines in a script, we can use loops to perform the same task. In this PHP Tutorial we will learn about the three looping statements.

In PHP, we have the following looping statements:

  • while – loops through a block of code as long as the specified condition is true
  • do…while – loops through a block of code once, and then repeats the loop as long as the specified condition is true
  • for – loops through a block of code a specified number of times

The While Loop

The while loop executes a block of code as long as the specified condition is true.

<?php 
$a = 3;
// while iterative structure 
while($a <= 5)
{
echo "The number is: $a <br>";
$a++;
?> 

The do..while Loop

The do…while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true.

<?php 
$a = 3;
// do-while iterative structure
do
{
echo "The number is: $a <br>";
$a++;
}
while ($a <= 5);
?> 

The For Loop

PHP for loops execute a block of code a specified number of times. It is used when you know in advance how many times the script should run.

<?php 
// For loop 
for ($x = 0$x <= 10$x++)
{
echo "The number is: $x <br>";
?>

Now that you have learnt about the Conditional Statements and Loops in PHP, let’s move ahead with the PHP Tutorial and learn about the Functions in PHP.

foreach Loop

The foreach statement is used to loop through arrays. For each pass the value of the current array element is assigned to $value and the array pointer is moved by one and in the next pass next element will be processed.

Syntax

foreach (array as value)
{
code to be executed;
}

example of foreach loop

<?php
$fruits = array("mango""apple""papaya""lichi");
// foreach loop structure
foreach ($fruits as $value)
{
echo "$value \n";
}
?>