三元运算符在 godaddy php 中不起作用(解析器错误)

发布于 2024-11-16 09:11:52 字数 1753 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挖鼻大婶 2024-11-23 09:11:52

三元运算符(顾名思义)通常需要 3 个参数,

$var = $expr ? $trueValue : $falseValue;

在 PHP5.3 中允许省略 $trueValue。在这种情况下,它的 $expr 用于它

$var = $expr ? : $falseValue;
// same as
$var = $expr ? $expr : $falseValue;

您的服务器上可能没有 PHP5.3。正如您在我的示例中看到的,修复此问题并为 5.3 之前的版本做好准备非常容易

$conditions = ($this->input->post()) 
            ? ($this->input->post()) 
            : array('tutor'=>$this->session->userdata('user_id'));

The ternary operator (as the name suggests) usually expects 3 arguments

$var = $expr ? $trueValue : $falseValue;

With PHP5.3 its allowed to omit $trueValue. In this case its $expr is used for it

$var = $expr ? : $falseValue;
// same as
$var = $expr ? $expr : $falseValue;

You 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

$conditions = ($this->input->post()) 
            ? ($this->input->post()) 
            : array('tutor'=>$this->session->userdata('user_id'));
岛徒 2024-11-23 09:11:52

语法 ? : 您使用的只是 PHP 5.3。

设置默认值:

$conditions = ($this->input->post()) ? $this->input->post() : array('tutor'=>$this->session->userdata('user_id'));

The syntax ? : you are using is PHP 5.3 only.

Set a default value:

$conditions = ($this->input->post()) ? $this->input->post() : array('tutor'=>$this->session->userdata('user_id'));
月寒剑心 2024-11-23 09:11:52

接受的答案是正确的,但忽略了一个重要的一点:

$x = $a ? : $b; // valid in PHP 5.3

确实应该替换为

$x = $a ? $a : $b; // valid in older versions of PHP

但是,当您处理函数时,而不是变量,请注意副作用:

$conditions = ($this->input->post()) 
        ? ($this->input->post()) 
        : array('tutor'=>$this->session->userdata('user_id'));

在上面的情况下,如果$this->input->post()返回一个真值,该函数将再次执行,这可能不是你想要什么。通过将三元运算符展开为其完整形式,您可以更清楚地看到这一点:

if ($this->input->post()) {
    $conditions = $this->input->post();
} else {
    $conditions = array('tutor' => $this->session->userdata('user_id'));
}

您可以看到该函数在上面的第一行和第二行执行。如果您不想这样做,请尝试以下操作:

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:

$conditions = ($this->input->post()) 
        ? ($this->input->post()) 
        : array('tutor'=>$this->session->userdata('user_id'));

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:

if ($this->input->post()) {
    $conditions = $this->input->post();
} else {
    $conditions = array('tutor' => $this->session->userdata('user_id'));
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文