Re: [Proposal] Modification to ?: functionality

From: Date: Tue, 26 Nov 2013 14:58:46 +0000
Subject: Re: [Proposal] Modification to ?: functionality
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-70403@lists.php.net to get a copy of this message
Hi Chris, On Tue, Nov 26, 2013 at 3:43 PM, Chris London <me@chrislondon.co> wrote: > I find in a lot of my code I have ternaries that set default values. I > also find that I can't use the ?: syntax for one reason and one reason > only. It throws a notice if the value isn't set. > > I believe these two statements are functionally equivalent: > > $foo = $foo ? $foo : 'default'; > > $foo = $foo ?: 'default'; > > I would like to change it so it also checks for isset() so I propose the > following would be functionally equivalent: > > $foo = isset($foo) && $foo ? $foo : 'default'; > > $foo = $foo ?: 'default'; > > I would be happy to see a patch for that, last time I checked it was hard to make the grammar happy with this change. Note that it's not necessarily a variable. It should have no impact on: $foo = gee() ?: "default"; > Random addition: what if we also allowed the following shorthand? > > $foo ?= 'default'; > > or possibly: > > $foo ?:= 'default'; > > Thanks everyone, > Chris > -- Etienne Kneuss http://www.colder.ch

« previous php.internals (#70403) next »