Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I was recently reviewing some of my code and noticed that in a fit of absent-mindedness, I'd left a structure like the following:

$guid = empty($subscription->guid) ?  : $subscription->guid;

Now, this wasn't doing what it's supposed to and is wrong, but since that property is always set now it was working fine, and there's no syntax error since 5.3 because of the following change:

Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.

I wasn't aware of this change, and now I'm curious if I should be using it or not. This is something I was sorely missing from languages like ruby where you can do eg, a = b || c to get either b or c rather than a 'real' boolean. However, the syntax they've chosen for the ternary operator seems a little counter intuitive to me. Should I be using this in production code? It definitely threw myself when I saw it by accident.

share|improve this question

4 Answers 4

up vote 7 down vote accepted

The ternary conditional operator without the second argument is a bit novel, but it is similar1 to the null-coalescing operator found in other Algol-derivative languages like C# and Perl, and as you mention, the || operator in Ruby (and JavaScript).

It looks weird at first, but it's not too out there (especially since there's precedent for similar operators in other languages), and can save a good deal of keystrokes. And, if what happened with the namespace delimiter (\) is any indication, weird syntaxes will eventually be adopted by the PHP community.

But one of the major problems PHP applications tend to face is the (sometimes) excruciatingly long lag time between release of a new version of PHP and when hosts start to support it. This leads to issues where you need to be backwards compatible with older versions of PHP, forgoing the use of convenience changes like this.

If that's not a concern to you, and your team agrees on its usage (or if you're a solo developer, if you're comfortable with it), by all means go for it. Like the namespace delimiter, I really think it's really going to be a matter of when, not if, it'll be acceptable in all future PHP projects.


Note 1: But not identical, given null-coalescing operators only test on non-null values (and not truthy values like PHP), and the ?: syntax doesn't suppress undefined notices as you mentioned in the comments.

share|improve this answer
    
At the moment, my 'team' consists purely of me. Hosting isn't an issue as we generally host 98% of the sites we build, and we host on top of 5.3.6. I realise the functionality itself isn't so unusual, just the syntax for it which was the root of my question. There's also the issue where it doesn't act like a null-coalescing operator without more syntax (@ to hide notices about undefined things). –  Matthew Scharley Feb 18 '12 at 1:09
3  
@MatthewScharley If the namespace delimiter (\) debacle is any indication, weird syntax is not a barrier to widespread adoption in PHP. It really comes down to when, not if, you should start using it. –  user8 Feb 18 '12 at 1:12

To me, that looks like an error in the code. It looks wrong, and in general, I avoid using syntax that looks like you've made an error for the sake of saving a few keystrokes. When you (or someone else) goes back to read the code later, I think that line is going to trip you up and cause you to have to spend time parsing what it's doing, for what is essentially a simple operation.

share|improve this answer
    
The example given is wrong. A correct usage of the functionality as intended would be $foo = @$bar ?: 'baz' which is equivalent to $foo = (@$bar ? $bar : 'baz'). I do agree with your comments though. –  Matthew Scharley Feb 19 '12 at 8:02

Well, PHP has a boolean type and the || operator returns a boolen, so in PHP the result of $a || $b is a boolean, this is consistent with && as with && it makes little sense to return one or the other but makes sense to return true/false. Having all boolean operators return bools sounds quite logical, too.

Additionally this is no invention from PHP but following C, where many design elements of "classic" PHP are coming from. Quoting from §6.5.14 of the C99 standard:

The || operator shall yield 1 if either of its operands compare unequal to 0; otherwise, it yields 0. The result has type int.

And, well, whehter you should use ?: or not can't be answered in the general form, but mind: Best way to language is to use the constructs it provides and use the paradigms it suggests. There is little sense in writing Java code like C code, or the other way round ;-)

share|improve this answer

I think it's a very useful operator and should definitely be used since the language standard explicitly defines it.

Note that it's officially called the Elvis Operator in Grooy (why? just look at it closely!), and had been proposed to be introduced in Java 7.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.