【问题标题】:Why error with with if (($x = $y)['abc'] == 'test')?为什么 if (($x = $y)['abc'] == 'test') 会出错?
【发布时间】:2015-03-05 00:54:11
【问题描述】:

我有以下代码:

if (mysqli_num_rows($result) && ($value = mysqli_fetch_array($result))['id'] != $id)

我得到以下错误:

$Parse 错误:语法错误,在 [...] 中出现意外的 '['

但我不明白这一点!

if (mysqli_num_rows($result) && mysqli_fetch_array($result)['id'] != $id)

这很好,但我想保存mysqli_fetch_array 的结果。而且我认为如果没有第二个 If 子句,这很容易实现。

【问题讨论】:

标签: php variables if-statement setting


【解决方案1】:

你的括号在一个奇怪的地方:

if (mysqli_num_rows($result) && ($value = mysqli_fetch_array($result))['id'] != $id)

应该是:

if (mysqli_num_rows($result) && ($value = mysqli_fetch_array($result)['id']) != $id)

或者...我不确定这是否会避免语法错误,但如果您想保留所有结果,请尝试以下操作:

if (mysqli_num_rows($result) && (($value = mysqli_fetch_array($result))['id']) != $id)

我添加了更多括号,希望强制执行的操作顺序能让解释器更清楚。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-09
    • 1970-01-01
    • 2022-09-23
    • 1970-01-01
    • 2011-03-25
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    相关资源
    最近更新 更多