PHP Conditional Statements

Last Updated : May 19, 2021

PHP Conditional Statements are used to execute certain parts of the program when specific conditions are passed, i.e., If you want to run a code block when a certain condition is true and another block when it is false then you can use these statements. PHP has different control statements to perform many conditional operations; these statements are also known as Decision-making statements because it helps to make decision based on conditions. Please find them below in detail,

If Statement

The if statement is used to check the given conditions and execute the code block if those conditions are satisfied and return as true. If those specific conditions are not satisfied then the PHP pointer will be moved to the next line after this if statement.

Syntax

if (conditions) {
  //code block to execute if conditions are true
}

Please find below the examples,

<?php
$a = 10;
if($a < 8) {
  echo "It is less than 8. <br>";
}
if($a > 8 && $a != 15) {
  echo "Yes, its greater than 8 and not equal to 15. <br>";
}
if($a > 15) {
  echo "It is greater than 15";
}
?>

If..Else Statement

The If...Else statement is used to check the given one or more conditions and execute code block if conditions are satisfied to true and it will execute else part of code block if conditions are returns false. Both if block and else block should have a certain code block for execution, it will be executed based on the conditions’ boolean value.

Syntax

if (conditions) {
  //code block to execute if conditions are true
} else {
  //code block to execute if conditions are false
}

Please find below the examples,

<?php
$a = 15;
if($a > 20) {
  echo "It is greater than 20. <br>";
} else {
  echo "No, It is not greater than 20. <br>";
}
//another example
if($a > 10 && $a != 5 && $a < 25) {
  echo "Yes, $a satisfied all checking.";
} else {
  echo "No, $a satisfied all checking.";
}
?>

If…Elseif…Else Statement

This If...Elseif...Else statement is used to check multiple conditions, and once a specific condition is satisfied, PHP will execute that code block accordingly. If all the conditions are not satisfied, then the final else statement’s code block will be executed.

Syntax

if (condition-1) {
  //code block to execute if condition-1 is true
} elseif (condition-2) {
  //code block to execute if condition-2 is true
} else {
  //code block to execute if both conditions are false
}

Please find below the examples,

<?php
$result = 82;

if($result < 35) {
  echo "You failed the test.";
} elseif($result >=35 && $result <=50) {
  echo "You scored Third Class.";
} elseif($result >=50 && $result <=65) {
  echo "You scored Second Class.";
} elseif($result >65 && $result <=80) {
  echo "You scored First Class.";
} elseif($result >80) {
  echo "You scored Distinction.";
} else {
  echo "Not a valid result";
}
?>

Switch Statement

The Switch statement is used to check the multiple conditions like If...Elseif...Else statement. Its checks multiple case value until the conditions are satisfied and it will execute a specific code block. If nothing is satisfied, it will execute the default code block of the switch statement.

By default, even though the conditions are satisfied, it will execute all the case statements. To avoid this behavior, we have to use a break statement that breaks the code execution when the switch conditions are satisfied. We will learn more about the break statement in the next chapter.

Syntax

switch (condition) {
  case (statement-1):
    //code block to execute
    break;
  case (statement-2):
    //code block to execute
    break;
  default:
    //code block to execute
    break;
}

Please find below the example,

<?php
$result = 82;

switch($result) {
  case ($result < 35):
    echo "You failed the test.";
    break;
  case ($result >=35 && $result <=50):
    echo "You scored Third Class.";
    break;
  case ($result >=50 && $result <=65):
    echo "You scored Second Class.";
    break;
  case ($result >65 && $result <=80):
    echo "You scored First Class.";
    break;
  case ($result >80):
    echo "You scored Distinction.";
    break;
   default:
    echo "Not a valid result.";
    break;
}
?>

In this tutorial, we learned about the Conditional Statements available in PHP. These decision-making statements are beneficial when we want to check conditions and run some code blocks in PHP programming. We will use these statements in many places in our upcoming tutorial series. Please stay tuned and jump into the next chapter.