CodesCompiler
🔍
HTMLCSSJavaScriptPythonSQLPHPSEOPostsBooksKids About Contact 🚀 Get Started Free

PHP - Conditional Operators

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 asternary 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.

OperatorDescriptionExample? :Conditional ExpressionIf Condition is true ? Then value X : Otherwise value Y

Syntax of the Conditional Operator

The syntax to use the conditional operator is as follows −

PHP CODE
condition ? value_if_true : value_if_false

Ternary operators are especially useful for shorteningif-elsestatements 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 CODE
<?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 −

PHP CODE
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 CODE
<?php
   $num = 15;
   $result = ($num % 2 == 0) ? "Even" : "Odd";
   echo "Number $num is $result";
?>

Output

This will generate the below output −

PHP CODE
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 CODE
<?php
   $num = -5;
   $result = ($num > 0) ? "Positive" : (($num < 0) ? "Negative" : "Zero");
   echo "Number $num is $result";
?>

Output

This will create the below output −

PHP CODE
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 CODE
<?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 −

PHP CODE
The maximum number is 80