Php 如何在三元运算符中只计算一次函数?

Php 如何在三元运算符中只计算一次函数?,php,performance,memory,ternary-operator,Php,Performance,Memory,Ternary Operator,我想知道我是否可以制作一个检查函数返回值的单行三元运算符,并使用它 让我们检查一下这个示例(PHP)代码: 我的目的是返回get\u db\u row(),但如果它是空的,则返回一个空行 但是,我想,此行将调用get\u db\u row()两次。是这样吗 我想叫它一次。一种解决方案是将返回值存储在如下变量中: $row = get_db_row($sql_parameters); return $row ? $row : get_empty_row(); 但是我能用一行来做吗 比如: ret

我想知道我是否可以制作一个检查函数返回值的单行三元运算符,并使用它

让我们检查一下这个示例(PHP)代码:

我的目的是返回
get\u db\u row()
,但如果它是空的,则返回一个空行

但是,我想,此行将调用
get\u db\u row()
两次。是这样吗

我想叫它一次。一种解决方案是将返回值存储在如下变量中:

$row = get_db_row($sql_parameters);
return $row ? $row : get_empty_row();
但是我能用一行来做吗

比如:

return ($row = get_db_row()) ? $row : get_empty_row();
可能吗

谢谢你的帮助

return get_db_row($sql_parameters) ?: get_empty_row();
如果您运行的是不支持此功能的早期版本的PHP

return ($x = get_db_row($sql_parameters)) ? $x : get_empty_row();
应该很好用

如果您运行的是不支持此功能的早期版本的PHP

return ($x = get_db_row($sql_parameters)) ? $x : get_empty_row();
应该很好用

如果您运行的是不支持此功能的早期版本的PHP

return ($x = get_db_row($sql_parameters)) ? $x : get_empty_row();
应该很好用

如果您运行的是不支持此功能的早期版本的PHP

return ($x = get_db_row($sql_parameters)) ? $x : get_empty_row();

应该没问题。

你说得对。以下行只调用函数一次:

return ($row = get_db_row()) ? $row : get_empty_row();
一些代码可以证明这一点:

$counter = 0;
function test() {
    global $counter;
    $counter++;
    return true;
}

$var = ($ret = test()) ? $ret : 'bar';
echo sprintf("#1 called the function %d times\n", $counter);

$counter = 0;
$var = ($ret = test()) ? test() : 'bar';
echo sprintf("#2 called the function %d times", $counter);
产出:

#1 called the function 1 times
#2 called the function 2 times

你说得对。以下行只调用函数一次:

return ($row = get_db_row()) ? $row : get_empty_row();
一些代码可以证明这一点:

$counter = 0;
function test() {
    global $counter;
    $counter++;
    return true;
}

$var = ($ret = test()) ? $ret : 'bar';
echo sprintf("#1 called the function %d times\n", $counter);

$counter = 0;
$var = ($ret = test()) ? test() : 'bar';
echo sprintf("#2 called the function %d times", $counter);
产出:

#1 called the function 1 times
#2 called the function 2 times

你说得对。以下行只调用函数一次:

return ($row = get_db_row()) ? $row : get_empty_row();
一些代码可以证明这一点:

$counter = 0;
function test() {
    global $counter;
    $counter++;
    return true;
}

$var = ($ret = test()) ? $ret : 'bar';
echo sprintf("#1 called the function %d times\n", $counter);

$counter = 0;
$var = ($ret = test()) ? test() : 'bar';
echo sprintf("#2 called the function %d times", $counter);
产出:

#1 called the function 1 times
#2 called the function 2 times

你说得对。以下行只调用函数一次:

return ($row = get_db_row()) ? $row : get_empty_row();
一些代码可以证明这一点:

$counter = 0;
function test() {
    global $counter;
    $counter++;
    return true;
}

$var = ($ret = test()) ? $ret : 'bar';
echo sprintf("#1 called the function %d times\n", $counter);

$counter = 0;
$var = ($ret = test()) ? test() : 'bar';
echo sprintf("#2 called the function %d times", $counter);
产出:

#1 called the function 1 times
#2 called the function 2 times

您的最后一个示例应该可以正常工作。将get_db_row()的结果赋给$row变量,同时对其求值。你试过了吗?这是判断它是否有效的最好方法。您的最后一个示例应该可以很好地工作。将get_db_row()的结果赋给$row变量,同时对其求值。你试过了吗?这是判断它是否有效的最好方法。您的最后一个示例应该可以很好地工作。将get_db_row()的结果赋给$row变量,同时对其求值。你试过了吗?这是判断它是否有效的最好方法。您的最后一个示例应该可以很好地工作。将get_db_row()的结果赋给$row变量,同时对其求值。你试过了吗?这是检验它是否有效的最好方法。