了解致命错误:无法在写入上下文中使用临时表达式

2022-08-30 14:32:08

我想确切地了解这个错误在PHP中意味着什么:如何故意创建错误以及如何避免或修复它。在下面的简单示例中,我得到了这个错误:

致命错误:无法在第 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'];

我的期望是:

确保正确处理此异常

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

注意:未定义的变量


答案 1

您忘记在单词之前包含 .$response

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

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

您应该将其更改为:

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

希望这有帮助。


答案 2

推荐