Re: Ternary operator enhancement for fluent code

From: Date: Tue, 04 Jul 2000 23:28:08 +0000
Subject: Re: Ternary operator enhancement for fluent code
References: 1 2 3  Groups: php.dev 
Request: Send a blank email to php-dev+get-23514@lists.php.net to get a copy of this message
> > doesn't look like line noise. For example, instead of $foo = $a ? $b; which > > would set $foo to $a if $a wasn't false and $b if $a was false, even > > resusing or again to produce something like $foo = $a or $b; seems cleaner. > > Even better might be a function which would return the first parameter which > > isn't false: $foo = first_valid($a, $b, $c, $d ...). This would be handy for > > more than simple binary work, too: > > > > $text_color = first_valid(@$MemberSetting["PageColor"], > > @$InstallationSetting["PageColor"], "WHITE"); > > > > The other advantage, of course, is that this wouldn't require any changes to > > the core language, which is why I wrote > > http://gateway.digitaria.com/~chris/php/first_valid/ back > > when I needed it. > > Maybe you´ve got the motivation to create some enhancement report? > A function that returns the first variable sounds to have big chances to > be implemented... > I´d welcome 1) one function which checks for identity against FALSE > and another function 2) that performs an isset() on each var Even if it's not part of the PHP core language, it's certainly easy to put it in an include file. Right now both of the functions I provide at http://gateway.digitaria.com/~chris/php/first_valid/ simply do a !empty() check. One function checks a list of variables and the other checks a bunch of arrays to find the first which has a !empty() value for a given key. It would take very little time to modify either function to use something other than !empty(). Chris

« previous php.dev (#23514) next »