This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
if (!$conditions = $this->input->post()) {
// Single equal sign in an if condition: make assignment, and check
// whether the result is truthy.
$conditions = array('tutor' => $this->session->userdata('user_id'));
}
这是我自己的代码中相当常见的模式。这只会执行函数 $this->input->post() 一次。如果结果为真,则该结果存储在 $conditions 中。如果结果不为真,则运行 if 条件内的代码。这会将回退值分配给 $conditions。好处是,无论哪种情况,$this->input->post() 仅运行一次。
The accepted answer is correct, but misses one important point:
$x = $a ? : $b; // valid in PHP 5.3
should indeed be replaced with
$x = $a ? $a : $b; // valid in older versions of PHP
However, when you're dealing with functions, not variables, be aware of side-effects:
In the above case, if $this->input->post()returns a truthy value, the function will be executed again, which may not be what you want. You can see this more clearly by expanding the ternary operator to its full form:
You can see that the function is executed on lines one and two above. If you don't want that, try this instead:
if (!$conditions = $this->input->post()) {
// Single equal sign in an if condition: make assignment, and check
// whether the result is truthy.
$conditions = array('tutor' => $this->session->userdata('user_id'));
}
This is a fairly common pattern in my own code. This will execute the function $this->input->post() only once. If the result is truthy, that result is stored in $conditions. If the result is not truthy, the code inside the if condition is run. This assigns the fallback value to $conditions. The benefit is that, in either case, $this->input->post() is run only once.
发布评论
评论(3)
三元运算符(顾名思义)通常需要 3 个参数,
在 PHP5.3 中允许省略
$trueValue
。在这种情况下,它的$expr
用于它您的服务器上可能没有 PHP5.3。正如您在我的示例中看到的,修复此问题并为 5.3 之前的版本做好准备非常容易
The ternary operator (as the name suggests) usually expects 3 arguments
With PHP5.3 its allowed to omit
$trueValue
. In this case its$expr
is used for itYou probably don't have PHP5.3 on your server. As you can see in my example its quite easy to fix this and make it ready for pre-5.3
语法
? :
您使用的只是 PHP 5.3。设置默认值:
The syntax
? :
you are using is PHP 5.3 only.Set a default value:
接受的答案是正确的,但忽略了一个重要的一点:
确实应该替换为
但是,当您处理函数时,而不是变量,请注意副作用:
在上面的情况下,如果
$this->input->post()
返回一个真值,该函数将再次执行,这可能不是你想要什么。通过将三元运算符展开为其完整形式,您可以更清楚地看到这一点:您可以看到该函数在上面的第一行和第二行执行。如果您不想这样做,请尝试以下操作:
这是我自己的代码中相当常见的模式。这只会执行函数
$this->input->post()
一次。如果结果为真,则该结果存储在$conditions
中。如果结果不为真,则运行if
条件内的代码。这会将回退值分配给$conditions
。好处是,无论哪种情况,$this->input->post()
仅运行一次。The accepted answer is correct, but misses one important point:
should indeed be replaced with
However, when you're dealing with functions, not variables, be aware of side-effects:
In the above case, if
$this->input->post()
returns a truthy value, the function will be executed again, which may not be what you want. You can see this more clearly by expanding the ternary operator to its full form:You can see that the function is executed on lines one and two above. If you don't want that, try this instead:
This is a fairly common pattern in my own code. This will execute the function
$this->input->post()
only once. If the result is truthy, that result is stored in$conditions
. If the result is not truthy, the code inside theif
condition is run. This assigns the fallback value to$conditions
. The benefit is that, in either case,$this->input->post()
is run only once.