PHP Decision Making/Conditional Statements
Tags: webdev, opensource, php, beginners
Published: 2019-06-20 07:52:09

As human beings we are faced regularly with making one decision or the other. The results of the decisions we make is aimed at solving the challenges we encounter. In PHP there is no difference at all, decision making can also be handled by the PHP programming language thereby making it possible for a particular process to be achieved even without the help of a human being.

Decisions work based on choices. With each choice, is attached an underlining process which should be executed whenever that choice is picked. Below we will be discussing and explaining some various forms of decision making in the PHP programming language. Follow me gradually and you will get every bit of information along the way.

Types of PHP Conditional Statements

1. The if statement executes some code if one condition is true.
Syntax
if (condition) {
    code to be executed if condition is true;
}

Example
<?php
$t = date("H");

if ($t < "20") {
    echo "Have a good day!";
}
?>

2. The if...else Statement
The if else statement is used to execute a certain code if a condition is true, and another code if the condition is false.
Syntax
if (condition) {
    code to be executed if condition is true;
} else {
    code to be executed if condition is false;
}

Example
<?php
$x = 10;
$y = 20;
if ($x >= $y) {
   echo $x;
} else {
   echo $y;
}
?>

3. The if...elseif...else Statement
Use the if...elseif...else statement to specify a new condition to test, if the first condition is false.
Syntax
if (condition) {
    code to be executed if this condition is true;
} elseif (condition) {
    code to be executed if first condition is false and this condition is true;
} else {
    code to be executed if all conditions are false;
}

Example
<?php
$age = 21;
if ($age<=13) {
   echo "Child.";
} elseif ($age>13 && $age<19) {
   echo "Teenager";
} else {
   echo "Adult";
}
?>

4. The Nested if Statement
Nested if statements means an that there's an if block inside another if block i.e. a control statement inside another control statement.
Syntax
if (condition 1)
{
	if (condition 2) {
	code to be executed if this condition is true;
} else {
	code to be executed if this condition is true;
}
} else {
if (condition 2)
{
	code to be executed if this condition is true;
} else {
	code to be executed if this condition is true;
}
}

5. The Switch Case Statement
The switch statement is an alternative to the if-elseif-else statement. Use the switch statement to select one of a number of blocks of code to be executed.
Syntax
switch (n) {
  case value1:
    //code to be executed if n=value1
    break;
  case value2:
     //code to be executed if n=value2
     break;
  ...
  default:
    // code to be executed if n is different from all labels
}

Example
$today = 'Tue';

switch ($today) {
  case "Mon":
    echo "Today is Monday.";
    break;
  case "Tue":
    echo "Today is Tuesday.";
    break;
  case "Wed":
    echo "Today is Wednesday.";
    break;
  case "Thu":
    echo "Today is Thursday.";
    break;
  case "Fri":
     echo "Today is Friday.";
     break;
  case "Sat":
     echo "Today is Saturday.";
     break;
  case "Sun":
     echo "Today is Sunday.";
     break;
  default:
     echo "Invalid day.";
}

6. The Jump Statements
Sometimes you may want to let the loops start without any condition, and allow the statements inside the brackets to decide when to exit the loop. There are two special statements that can be used inside loops: Break and Continue.

- Break Statement: This ends execution of the current structure. Break accepts an optional numeric argument which tells it how many execution of nested structures to be interrupted.

Example
<?php
   $x = 0 ;
   while($x)
   {
     echo($x . " ");
      if($x == 5)
      {
         break;
      }
      $x ++ ;
   }
  // output 0 1 2 3 4 5  
?>

- Continue Statement: This is used to stop processing the current block of code in the loop and goes to the next iteration. The Continue statement as well as the Break can take an optional numeric argument which tells it how many levels of enclosing loops it should skip to the end of.

Example
<?php
   for($i = 0; $i < 5; $i ++)
   {     
      if($x == 2)
      {
         continue;
      }
      echo $i . " ";
   }
  // output 0 1  3 4 
?>

- Exit Statement: An exit statement is used to terminate the current execution flow. As soon as an exit statement is found, it will terminate the program.

Example
<?php 
$a=5; 
$b=5.0; 
if($a==$b) 
 { 
    //terminating script with a message using exit() 
    exit('variables are equal'); 
 } 
else
 { 
   //terminating script with a message using exit() 
   echo "variables are not equal";
 } 
?>