Re: Combined assignment operator for short ternary
| From: | Sebastian Krebs | Date: | Fri, 24 Aug 2012 23:43:18 +0000 |
| Subject: | Re: Combined assignment operator for short ternary | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-62465@lists.php.net to get a copy of this message | ||
Am 24.08.2012 23:11, schrieb Ferenc Kovacs:
On Fri, Aug 17, 2012 at 11:41 PM, Sebastian Krebs <krebs.seb@gmail.com <mailto:krebs.seb@gmail.com>> wrote:Couldn't believe it, thus I tested it myself $ php -a Interactive mode enabled <?php $f = $f ?: 'bar'; PHP Notice: Undefined variable: f in - on line 2 PHP Stack trace: PHP 1. {main}() -:0 Notice: Undefined variable: f in - on line 2 Call Stack:Hi,Don't know, how complicated this is (and also someone (not me) must implement it, because I can't :X), but to be in sync with the operators the short ternary operator should be usable in conjunction with the assignment like the other binary operators. Don't know, if anybody understands me :D So here is an example// instead of $foo = $foo ?: 'default'; // Just $foo ?:= 'default';would this trigger a notice if $foo is not defined? if yes, then it would be different from the current behavior of the ternary operator.
8.9779 619512 1. {main}() -:0
Don't know, what you are talking about, but the notice _is_ the current behaiour and therefore: No difference.
if no, then I would never use this. I mean if I have to set the variable before the check, then I would put the check into the assignment.The main thought about it was function foo ($bar = null) {
$bar = $bar ?: 'default';} I _always_ use 'null' as default - If you want to omit a parameter, but want to set one after that, you don't need to look whats the default: It's 'null' - Ive often enough seen something like function foo ($limit = 10) { /* code */ } // Somewhere else function bar ($limit = 50) { /* code */ $foo($limit); /* code */} // Even somewhere else bar(); Imagine there are some other methods and functions in between it's quite annoying.
-- Ferenc Kovács @Tyr43l - http://tyrael.hu