top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is the ternary conditional operator in PHP?

0 votes
521 views
What is the ternary conditional operator in PHP?
posted Jun 16, 2014 by Amanpreet Kaur

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

0 votes

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
For more information:http://forum.codecall.net/topic/51638-the-ternary-operator-in-php/

answer Jun 17, 2014 by Vrije Mani Upadhyay
0 votes

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;

answer Jun 18, 2014 by Rahul Mahajan
Similar Questions
+1 vote

What is the "->" operator for functions called?

At the bottom of PHP: Operators - Manual (http://php.net/manual/en/language.operators.php) is the note:

"The -> operator, not listed above, is called "object operator" (T_OBJECT_OPERATOR)."

It has been down-voted 81 times. Is that because the operator is not called the "object operator"? Or is it because the note says that the operator is not listed?

I apologize if my criticism is not appropriate, but the operator is not properly documented. The "->" operator should be listed among the other PHP operators and there should be documentation of it.

The C++ standard calls the "." and "->" operators the "dot" and "arrow" operators, correspondingly. For example, see "5.2.4 Pseudo destructor call" in: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf
That is the latest draft of the newest C++ standard.

PHP can call its "->" operator whatever the designers decide, but there should be a definition to eliminate the confusion of different names depending on personal preference. A book I am reading calls the operator the "arrow operator". Is it called the object operator (as implied by the tag) or the arrow operator or something else?

...