Scott Klarr Jr
Shorthand PHP IF statement without the else notation
In an earlier post I explained how to do Ternary shorthand if/else statements in PHP. One of the major benefits of using a shorthand if/else statement is that you can use it inline with a string. But what if you only need to do in inline if statement, without the else?
Most people would just do the standard shorthand notation and make the else return a blank string like this:
$string = "The car is ".($carDead==true?"NOT":"")." working.";
If you're kind of anal about how your code looks, you might not like doing this because it's redundant and makes the code less readable (not by much, but still). Doing a quick search on Google, I found that many people have asked if theres a way to do a shorthand if without the else. The straight answer is no, PHP does not offer such notation. However, I have a solution.
PHP allows you to execute functions inline with a string and the returned value of that function will be inserted into the string. We can use this to create a pseudo inline if statement. Here is the function:
function iif($statment,$returnTrue=true,$returnFalse=false) {
if($statement) return $returnTrue;
else return $returnFalse;
}
Now you can load the function iif anytime you need to do an inline if statement without an else (but it also has the built in ability for a specified false return as well by including a third parameter)
$string = "The car is ".iif($carDead==true,"NOT")." working";
This code will not execute as quickly as a straight if/else statment, but I'm sure the performance difference is so minimal that it would not make a significant difference unless you were processing thousands of inline statements using this function.



Chris Bourton Nov 13, 2008
I don't actually see that as an improvement. As you pointed out it could possibly be slower. But also the number of characters needed to type hasn't actually changed. I know it's just an example, but the 3 you lost within the inline :"" was gained back with the name of the function.
Considering this, when would this be useful? I'm not sure, unless it's just a pet hate to have in your code "" with nothing in them.
Scott Klarr Nov 13, 2008
I used to use this method a lot but ended up dropping it over time for just using the standard ternary with a blank else return. The primary reason I made this post was to provide a straight answer and solution for people searching Google. The extra :"" does still bug me a little but its not a huge deal.
Shawn Jan 19, 2009
Old post but still relavent. In PHP 5.3 there will be an optional ternary.
In < 5.3:
$blah = $someVar ? $someVar : false;
In 5.3:
$blah = $someVar ?: false;
Benoit Mar 16, 2009
Very smart approach indeed!
tom3k May 31, 2009
Anyone have any idea which of the following would be the fastest?
:false
:''
:null
I understand the differences would be absolutley minimal but this google search has been my improve my coding daily routing for today ;)
Oh, and I mean more in the sense:
$boh='Blah: '.($blah?'123':'');
I have, and still, always use :''...
Yeni Diziler Mar 02, 2010
Thank you very much for your geat post...