Using the Ternary Operator in PHP
by c.bavota | Posted in Tutorials | 20 comments
It took me a while to actually figure out the name of this type of operator since trying to search “PHP” and “question mark” didn’t really result in anything useful. You’ve probably seen the ternary operator used multiple times in code and just never really understood what it was and how it worked. All it is, is a simpler alternative to setting a variable using an if-else conditional statement.
We can take the following if-else conditional statement:
if ( 'on' == $value )
$check = 'It is on';
else
$check = 'It is off';
And replace it with a ternary operator to get this:
// The parentheses around the conditional statement are not required // I just like to use them as a visual guide $check = ( 'on' == $value ) ? 'It is on' : 'It is off';
That breaks down to:
$variable = ( condition ) ? true : false;
If the value of our $value variable equals ‘on’, then our $check variable will equal to ‘It is on’. Otherwise, it will equal to ‘It is off’.
Once you grasp how the ternary operator works it is a great tool to use to trim down your code and simplify your conditional statements.
Read more about the ternary operator on the PHP website.



I didn’t even know this existed; thanks.
i don’t think such a code like this exits ! thansk !
That code also performs better than traditional condition statement.
You really make it appear so easy with your presentation however I in finding this matter to be actually one thing which I think I would
by no means understand. It sort of feels too complex and extremely large for me.
I’m looking ahead on your next publish, I’ll attempt to get the grasp of it!
It’s also great for keeping your code error-free when defining variables (those irritating “variable undefined” PHP errors):
If $other_variable is set and not NULL, it will be assigned to $variable. If not, $variable will quietly be set to NULL with no errors.
@ Aquiles I used $check = ( ‘on’ == $value ) ? ‘It is on’ : ‘It is off’; and it does indeed work
I’m looking ahead on your next publish, I’ll attempt to get the grasp of it!
Great!
And using parenthesis this is the best practice.
Thank you !!
Wow, that was easy to understand. thanks!
This is the most used trick in all of my scripts, it makes the code look so nice and tiny!
Definitely a useful tool to use *as long as the conditional is tiny*. If you’re doing anything more complex than what is in this example, I’d avoid ternary operators, as they can be really hard to follow (as opposed to a nice, clean, bracketed block).
In the context here though, it’s great.
Been using this for a while myself. Nice one for sharing though
This is a great way to minimise the amount of code used. I like using this when assigning values to nested array items i.e.
Thank you.This article is very useful.I am working to make my page
Been using this for a while myself.
If I use SWITCH method is there any better than “IF” method
switch ($value == ‘on’){
case true:
$check = ‘It is on’;
break;
case false:
$check = ‘It is off’;
break;
}
The SWTICH method is very useful when you have multiple condition. For a simple boolean, I usually go for the TERNARY operator.
I think so too. You should only use the switch statement when you have more expression and youe do not have to check the result… when you have visitor statements like POST or GET check if the value is set with the isset function. When you want a logical result of a variable like the value can only a or b or c then the switch statement is very usefully anotherwise you should try if statement …
Thanks for your post. I like this tut.
is true, also committed the same mistake, thanks for sharing, I will use from now on!