Edit report at https://bugs.php.net/bug.php?id=54106&edit=1

 ID:                 54106
 Comment by:         marrch dot caat at gmail dot com
 Reported by:        a at b dot c dot de
 Summary:            Shortcut "ternary" companion operator
 Status:             Open
 Type:               Feature/Change Request
 Package:            Scripting Engine problem
 Operating System:   Irrelevant
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[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

Reply via email to