Shorthand boolean IF with PHP

Twitter Facebook Pinterest Google+ Stumbleupon LinkedIn


This is a good alternative to using a basic IF condition. It basically fits the whole thing on a minimal line of code.

It is an  easy way to compare two values and select the right one. There are a few ways to use this but the basic jist of it is to compare two values and if its is true use the first value after the question mark, or if the condition returns false use the second value.

echo $targetvar =  ($somevar == "thisVar" ? "itIsTrue" : "itIsFalse");


In the example above we can set $targetvar with the condition. it will compare the first two variables with whatever you use, more then less then etc and it will choose the correct value from the two after the question mark.


Posted by Adi on February 4, 2009

Twitter Facebook Pinterest Google+ Stumbleupon LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *