我用PHP 7.1中的[]在PHP 5.6中创建了数组,但出现致命错误 [英] i created arrays in PHP 5.6 with [] in PHP 7.1 give fatal error
本文介绍了我用PHP 7.1中的[]在PHP 5.6中创建了数组,但出现致命错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在PHP 5.6中可以正常工作,但在PHP 7.1中会出现致命错误:未捕获错误:字符串不支持[]运算符
in PHP 5.6 this work fine but in PHP 7.1 give Fatal error: Uncaught Error: [] operator not supported for strings
$result->execute();
$result->bind_result($id, $name);
while($result->fetch()){
$datos[]=array(
$id => $name
);
}
推荐答案
从PHP 7.1开始,当您访问数组之类的非数组变量(在这种情况下为字符串)时,将引发致命错误.
As of PHP 7.1, when you access a non-array variable (in this case a string) like an array, a fatal error will be thrown.
首先使用$datos = [];
初始化数组.这将覆盖您之前设置的所有内容,并将此变量显式设置为数组:
Initialize the array first, with $datos = [];
. This will overwrite anything you have set earlier, and explicitly set this variable as an array:
$result->execute();
$result->bind_result($id, $name);
$datos = [];
while($result->fetch()){
$datos[]=array(
$id => $name
);
}
如果您尝试创建$id
=> $name
的数组,则以下代码应该有效:
If you're trying to create an array of $id
=> $name
, the following code should work:
$result->execute();
$result->bind_result($id, $name);
$datos = [];
while($result->fetch()){
$datos[ $id ] = $name;
}
这篇关于我用PHP 7.1中的[]在PHP 5.6中创建了数组,但出现致命错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文