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
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: a at b dot c dot de
New email:
PHP Version: OS:

 

 [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: Fri Apr 26 15:01:56 2024 UTC