Req #54106 [Opn->Wfx]: Shortcut "ternary" companion operator
| From: | aharvey@php.net | Date: | Tue, 28 Aug 2012 01:51:11 +0000 |
| Subject: | Req #54106 [Opn->Wfx]: Shortcut "ternary" companion operator | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-172964@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=54106&edit=1
ID: 54106
Updated by: aharvey@php.net
Reported by: a at b dot c dot de
Summary: Shortcut "ternary" companion operator
-Status: Open
+Status: Wont fix
Type: Feature/Change Request
Package: Scripting Engine problem
Operating System: Irrelevant
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2012-08-27 14:29:26] 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.
------------------------------------------------------------------------
[2011-02-26 15:55:07] 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.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=54106&edit=1