PHP - Conditional Operators Examples



You would use conditional operators in PHP when there is a need to set a value depending on conditions. It is also known as ternary operator. It first evaluates an expression for a true or false value and then executes one of the two given statements depending upon the result of the evaluation.

Ternary operators offer a concise way to write conditional expressions. They consist of three parts: the condition, the value to be returned if the condition evaluates to true, and the value to be returned if the condition evaluates to false.

Operator Description Example
? : Conditional Expression If Condition is true ? Then value X : Otherwise value Y

Syntax of the Conditional Operator

The syntax to use the conditional operator is as follows −

condition ? value_if_true : value_if_false

Ternary operators are especially useful for shortening if-else statements into a single line. You can use a ternary operator to assign different values to a variable based on a condition without needing multiple lines of code. It can improve the readability of the code.

However, you should use ternary operators judiciously, else you will end up making the code too complex for others to understand.

Example - Basic Usage of Conditional Operator

Try the following example to understand how the conditional operator works in PHP. Copy and paste the following PHP program in test.php file and keep it in your PHP Server's document root and browse it using any browser.

<?php
   $a = 10;
   $b = 20;

   /* If condition is true then assign a to result otherwise b */
   $result = ($a > $b ) ? $a :$b;

   echo "TEST1 : Value of result is $result \n";

   /* If condition is true then assign a to result otherwise b */
   $result = ($a < $b ) ? $a :$b;

   echo "TEST2 : Value of result is $result";
?>

Output

It will produce the following output −

TEST1 : Value of result is 20
TEST2 : Value of result is 10

Example: Check Even or Odd Number

Here we will use the conditional operator to check the even and odd numbers. And print the result as per the condition.

<?php
   $num = 15;
   $result = ($num % 2 == 0) ? "Even" : "Odd";
   echo "Number $num is $result";
?> 

Output

This will generate the below output −

Number 15 is Odd

Example 2: Check Positive, Negative or Zero

Now the below code checks the positive, negative and zero values by using the nested conditional operator. See the code below −

<?php
   $num = -5;
   $result = ($num > 0) ? "Positive" : (($num < 0) ? "Negative" : "Zero");
   echo "Number $num is $result";
?> 

Output

This will create the below output −

Number -5 is Negative

Example 3: Find the Maximum of Three Numbers

In the following example, we are showing that you can use the conditional operator for finding the maximum of three different numbers. See the example below −

<?php
   $x = 50;
   $y = 30;
   $z = 80;

   $max = ($x > $y) ? (($x > $z) ? $x : $z) : (($y > $z) ? $y : $z);

   echo "The maximum number is $max";
?> 

Output

Following is the output of the above code −

The maximum number is 80
Advertisements