php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #54106 Shortcut "ternary" companion operator
Submitted: 2011-02-26 15:55 UTC Modified: 2012-08-28 01:51 UTC
Votes:2
Avg. Score:4.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: a at b dot c dot de Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: Irrelevant OS: Irrelevant
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
22 + 32 = ?
Subscribe to this entry?

 
 [2011-02-26 15:55 UTC] a at b dot c dot de
Description:
------------
The expression $a ? $b : $c can be shortened to $a ?: $c if $b==$c.

In this the "?:" operator behaves much like the "||" operator in, say, Perl or JavaScript, or the "or" operator of Python: "$a if it looks true, $c otherwise".

But those three languages also have "&&" (or "and") operators which mean "$a if it looks false, $c otherwise".

PHP currently lacks such an operator: where Python can have "a and c", PHP still needs "$a ? $c : $a", including the potential double evaluation or intermediate variable needed if "$a" is anything more complex.

So the Feature Request is another operator, perhaps ":?", which behaves as described in the third paragraph above: "$a :? $c" would be an abbreviation for and equivalent to "$a ? $c : $a". It's possible to come up with a tortured bit of algebra to justify the ":?" symbol, but aesthetic symmetry ought to be enough of an excuse both for the symbol and more importantly the operator it identifies.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-08-27 14:29 UTC] marrch dot caat at gmail dot com
Looks like the description is a bit weird, or I just couldn't understand it. I see no meaning in expression $a ? $c ? $a - but I often use scripts like $a ? $a : $b - which means that if $a isn't empty, use it, otherwise use $b.

C# and some other languages have an ?? operator for that, and if it would exist in PHP, it'd be possible to write expressions like the following:
$connStr = $currConn ?? $config['defaultConnStr'] ?? requestUserForConnStr();
- which is enough clear and useful. Now it has to be written by either sequence of IFs, or by something like:
$connStr = $currConn ? $currConn : ($config['defaultConnStr'] ? $config['defaultConnStr'] : requestUserForConnStr());
- which isn't that clear.
 [2012-08-28 01:51 UTC] aharvey@php.net
I don't see much value in adding more line noise to the already line noisey variations on the ternary operator, personally. Write an RFC and post to Internals if you feel strongly about it.

@marrch.caat: You can already do that with $a ?: $b.
 [2012-08-28 01:51 UTC] aharvey@php.net
-Status: Open +Status: Wont fix
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 07 22:01:30 2024 UTC