Understanding Fatal error: Cannot use temporary expression in write context

Understanding Fatal error: Cannot use temporary expression in write context

我想了解这个错误在 PHP 中的确切含义:如何故意创建错误以及如何避免或修复错误。 在我下面的简单示例中,我收到了这个错误:

Fatal error: Cannot use temporary expression in write context in line 11

第 11 行是以下行:

response['error'] = "Error: B is less that C <br />";

错误代码如下:

$response = [];
$a = 4;
$b = 8;
$c = 9;
$d = 29;

if($a !== $b){
    $response['error'] = "Error: A is not equal to B <br />";
}elseif($b < $c){
    response['error'] = "Error: B is less that C <br />";
}
if($d > $c){
    response['success'] = "Success: D is greater than C<br />";
}

echo $response['error'];
echo $response['success'];

我的期望是:

确保正确处理此异常

我知道变量已定义,否则错误将是:

Notice: Undefined variable

您忘记在单词 response 前加上 $

这在第 11 行和第 14 行中很明显,如下所示:

response['error'] = "Error: B is less that C <br />";

您应该将其更改为:

$response['error'] = "Error: B is less that C <br />";

希望对您有所帮助。