Conditionally assigning PHP values

筅森魡賤 提交于 2019-12-12 07:24:48

问题


For the very common case of assigning a value to a variable based on the outcome of an expression I'm a fan of ternary operators:

$foo = $bar ? $a : b;

However, if $bar is a relatively expensive operation and I want to assign the result of $bar to $foo if the result is truthy, this is inefficient:

$foo = SomeClass::bigQuery() ? SomeClass::bigQuery() : new EmptySet();

One option is:

$foo = ($result = SomeClass::bigQuery()) ? $result : new EmptySet();

But I'd rather not have the extra $result sitting in memory.

The best option I've got is:

$foo = ($foo = SomeClass::bigQuery()) ? $foo : new EmptySet();

Or, without ternary operators:

if(!$foo = SomeClass::bigQuery()) $foo = new EmptySet();

Or, if program flow operators are not your style:

($foo = SomeClass::bigQuery()) || ($foo = new EmptySet());

So many options, non of them really satisfactory. Which would you use, and am I missing something really obvious here?


回答1:


PHP 5.3 introduced a new syntax to solve exactly this problem:

$x = expensive() ?: $default;

See the documentation:

Since PHP 5.3, it is possible to leave out the middle part of the ternary operator.
Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.




回答2:


Can you update SomeClass:bigQuery() to return a new EmptySet() instead of false?

Then you just have

$foo = SomeClass::bigQuery();



回答3:


A slight variation of your last option:

$foo = SomeClass::bigQuery() or new EmptySet(); this doesn't actually work, thanks for noticing.

Used often in combination with mySQL code, but seems always forgotten in comparable situations:

$result = mysql_query($sql) or die(mysql_error());

Although personally I prefer one you already mentioned:

if(!$foo = SomeClass::bigQuery())
    $foo = new EmptySet();



回答4:


$foo = SomeClass::bigQuery();
if (!$foo) $foo = new EmptySet();

Revision two, credit @meagar



来源:https://stackoverflow.com/questions/4329697/conditionally-assigning-php-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!