A programmer's paradise created by the twist of deliberated fantasies, and actions of a determined imagination.
Follow ME:
If you would like to make an appoinment, contact ME directly via my email address at tuso@programmerspride.com.

Add to Google
Verification Code
Verification Code / Security Code or similar to the CAPTCHA Project. With PHP GD Library, you can create a verification code to verify access o ...

The Ternary Conditional Operation in PHP

The ternary conditional operators are the ? and :

To demonstrate its use, first, we define our problem.
For example, our problem is to compare 2 numbers. If the first number is greater than the second, we will prompt "Hello", else, we say "Hi".

So here's the solution using If and Else statement:

$a = 4;
$b = 9;

if($a > $b)
   echo "Hello";
   echo "Hi";

In Ternary Operation, the solution is this:

echo ($a > $b) ? "Hello" : "Hi";

Ternary Operation explained: If the value of the first subexpression is TRUE (non-zero), then the second subexpression is evaluated, and that is the result of the conditional expression. Otherwise, the third subexpression is evaluated, and that is the value.