Core PHP

PHP Lesson 6: Conditionals

Overview

This lesson explains how the various kinds of conditional statements operate in PHP. A conditional is a command that executes a portion of code under certain conditions. The conditional statements that we look at here are the if, else if, else, the ternary operator, and the spaceship operator.

If and If Else

If statements use booleans values to determine whether a portion of code should be executed. In the case where the boolean value is true, the code is executed. Otherwise, it is not executed. Else statements can be added to if statements to provide an alternative path of execution. In that case, the if portion is evaluated when the boolean value is true, and the else portion is evaluated when the boolean value is false.

The program below assigns a variable the value 14. Then two if statements check whether that value is greater than 10 and greater than 20. Since only the first check is true, only that if block gets executed. After that, there is an if-else statement that checks whether the value of the variable is greater than 30. Since it is not, the else branch is executed.

Prog1.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net PHP</title>
  </head>
  <body>
<?php
$iNumber = 14;

echo "<pre>";
if ($iNumber > 10) {
  echo "The number $iNumber is greater than 10.<br />";
}
echo "</pre>";

echo "<pre>";
if ($iNumber > 20) {
  echo "The number $iNumber is greater than 20.<br />";
}
echo "</pre>";

echo "<pre>";
if ($iNumber > 30) {
  echo "The number $iNumber is greater than 30.<br />";
} else {
  echo "The number $iNumber is less or equal to 30.<br />";
}
echo "</pre>";
?>
  </body>
</html>
 
 

Output for Prog1.php

 

Else If

Like the else statement, we can create conditionals with multiple "else if" branches. An "else if" is executed when its statement is true and all of the previous if checks were false. In this program, an initial if checks whether the value is less than 10. Each subsequent check, determines whether the value is less than a progressively larger value. In this way, each condition determines a distinct interval that the value is contained in.

Prog2.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net PHP</title>
  </head>
  <body>
<?php
$iNumber = 14;

echo "<pre>";
if ($iNumber < 10) {
  echo "The number $iNumber is less than 10.<br />";
} else if ($iNumber < 12) {
  echo "The number $iNumber is greater than or equal to 10 and less than 12.<br />";
} else if ($iNumber < 14) {
  echo "The number $iNumber is greater than or equal to 12 and less than 14.<br />";
} else if ($iNumber < 16) {
  echo "The number $iNumber is greater than or equal to 14 and less than 16.<br />";
} else {
  echo "The number $iNumber is greater than or equal to 16.<br />";
}
echo "</pre>";
?>
  </body>
</html>
 
 

Output for Prog2.php

 

The Ternary Operator

The ternary operator is an operator that evaluates a boolean value and returns one value if it is true, and another value if it is false. The ternary operator take the form of a boolean expression, followed by a question mark, follow by a value, followed by a colon, and followed by a second value. In the program below, the variable sMessage is set to "less than" if iNumber is less than 10 and set to "greater than or equal to" otherwise. The string is then concatenated inside the output message.

Prog3.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net PHP</title>
  </head>
  <body>
<?php
$iNumber = 14;
$sMessage = (($iNumber < 10) ? "less than" : "greater than or equal to");

echo "<pre>";
  echo "The number $iNumber is ".$sMessage." 10.<br />";
echo "</pre>";
?>
  </body>
</html>
 
 

Output for Prog3.php

 

The Spaceship Operator

This last program demonstrates the spaceship operator. The spaceship operator is a binary comparison operator that returns -1 if the first value is less than the second, 0 if they are equal, and 1 is the first value is greater than the second. Here, the spaceship operator is applied to three pairs of values to demonstrate how it works.

Prog4.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net PHP</title>
  </head>
  <body>
<?php
echo "<pre>";
  echo $sMessage = " 3 <=> 5  -> ".(3 <=> 5)."<br />";
  echo $sMessage = " 5 <=> 5  -> ".(5 <=> 5)."<br />";
  echo $sMessage = " 7 <=> 5  -> ".(7 <=> 5)."<br />";
echo "</pre>";
?>
  </body>
</html>
 
 

Output for Prog4.php

 
 

© 2007–2024 XoaX.net LLC. All rights reserved.