top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Ternary conditional operator in php?

+2 votes
6,242 views

The Ternary comparison operator is an alternative to the if-else statement,
it's shorter and could be used to easily do a significant difference depending on a condition
without the need of a if statement. The Ternary operator is made for two different actions,
one if the condition is true, the other one if it's false but with nested Ternary operators
we can use it like an if-elseif.else statement(more to that later).
The syntax of a Ternary operator looks like this:

(condition) ? TRUE : FALSE 

The Ternary operator will return either the first value(if the condition were true) or the second value (if the condition were false), so here comes a simple example on how we can use it:

$siteFeedback = ($Site == "codecall") ? "Awesome" : "Unknown";

So what the example does is checking if $Site is equals to codecall, if it is "Awesome" will be stored in $siteFeedback but if it's not "Unknown" will be stored.

The above example would look like this with an if-else statement:

 if ($Site == "codecall") { 
      $siteFeedback = "Awesome";
  }else{ 
      $siteFeedback = "Unknown"; } 

Since the Ternary operator returns a value we can use it for everything, not only storing different values in variables.

echo "The current pupil " . (($pupils[$i] >= 15) ? "did" : "didn't") . " pass the test";

n the above example we're testing inside the echoing of a string if it should be "did" or "didn't" depending on how much points the pupil had.
In this case we had to put the Ternary operator inside parentheses to make it work.
In the example you saw how simple we could alter a string with a Ternary operator inside it.
With an if-else statement we would have to first store "did" or "didn't" in a variable and then put it there instead.
Or we could have wrote the same string twice with only the difference between "did" and "didn't" but that wouldn't have been so good if we wanted to alter the base string, then we would have to do it twice.

Syntax: truth_expr ? expr1 : expr2

Expression preceding the ? is evaluated, if it’s true, then the expression preceding the : is executed, otherwise, the expression following : is executed.
For example:
$a = 9;
$message = isset($a) ? '$a is set' : '$a is not set';
print $message;

posted Jun 18, 2014 by Vrije Mani Upadhyay

  Promote This Article
Facebook Share Button Twitter Share Button LinkedIn Share Button


Related Articles

It is also called the ternary operator and used for inline if-else mostly for assignment operations.

SYNTAX

Condition ? expression 2 : expression 3
If Condition is true expression 1 is returned 
If Condition is false expression 2 is returned

Example
Say we have following C/C++ if-else statement

if (a > b) {
    result = x;
} else {
    result = y;
}

This can be rewritten as the following statement using the conditional or ternary operator.

result = a > b ? x : y;

Differences between C and C++ behavior

The conditional operator in C++ can return an lvalue, whereas C does not allow for similar functionality. Hence, the following is legal in C++:

(true ? a : b) = 1;

To replicate this in C, you would have to resort to if/else, or deal with references directly:

*(true ? &a : &b) = 1;
READ MORE
...