非法字符串偏移警告PHP
在将PHP版本更新到5.4.0-3之后,我遇到了一个奇怪的PHP错误 我有这个阵列:非法字符串偏移警告PHP,php,warnings,Php,Warnings,在将PHP版本更新到5.4.0-3之后,我遇到了一个奇怪的PHP错误 我有这个阵列: Array ( [host] => 127.0.0.1 [port] => 11211 ) 当我尝试这样访问它时,会收到奇怪的警告 print $memcachedConfig['host']; print $memcachedConfig['port']; Warning: Illegal string offset 'host' in .... Warning: I
Array
(
[host] => 127.0.0.1
[port] => 11211
)
当我尝试这样访问它时,会收到奇怪的警告
print $memcachedConfig['host'];
print $memcachedConfig['port'];
Warning: Illegal string offset 'host' in ....
Warning: Illegal string offset 'port' in ...
我真的不想只编辑我的php.ini并重新设置错误级别。请尝试这种方式。。。。我已经测试了这个代码。。。。它起作用了
$memcachedConfig = array("host" => "127.0.0.1","port" => "11211");
print_r($memcachedConfig['host']);
请这边试试。。。。我已经测试了这个代码。。。。它起作用了
$memcachedConfig = array("host" => "127.0.0.1","port" => "11211");
print_r($memcachedConfig['host']);
在…中出现的错误
非法字符串偏移量“whatever”通常意味着:您试图将字符串用作完整数组
这实际上是可能的,因为字符串在php中可以被视为单个字符的数组。因此,您认为$var是一个带有键的数组,但它只是一个带有标准数字键的字符串,例如:
$fruit_counts = array('apples'=>2, 'oranges'=>5, 'pears'=>0);
echo $fruit_counts['oranges']; // echoes 5
$fruit_counts = "an unexpected string assignment";
echo $fruit_counts['oranges']; // causes illegal string offset error
function testimonial($id); // This function expects $id as an integer
您可以在此处看到这一点:
对于那些试图将模糊的错误转化为解决问题的人,就像我一样。错误中的非法字符串偏移量“whatever”通常意味着:您试图将字符串用作完整数组
这实际上是可能的,因为字符串在php中可以被视为单个字符的数组。因此,您认为$var是一个带有键的数组,但它只是一个带有标准数字键的字符串,例如:
$fruit_counts = array('apples'=>2, 'oranges'=>5, 'pears'=>0);
echo $fruit_counts['oranges']; // echoes 5
$fruit_counts = "an unexpected string assignment";
echo $fruit_counts['oranges']; // causes illegal string offset error
function testimonial($id); // This function expects $id as an integer
您可以在此处看到这一点:
对于那些试图将模糊的错误转化为解决问题的人,就像我一样。在检查数组之前,请执行以下操作:
if(!is_array($memcachedConfig))
$memcachedConfig = array();
在检查阵列之前,请执行以下操作:
if(!is_array($memcachedConfig))
$memcachedConfig = array();
您正试图访问一个字符串
,就像它是一个数组一样,使用的键是字符串
<代码>字符串
无法理解这一点。在代码中,我们可以看到问题:
"hello"["hello"];
// PHP Warning: Illegal string offset 'hello' in php shell code on line 1
"hello"[0];
// No errors.
array("hello" => "val")["hello"];
// No errors. This is *probably* what you wanted.
深入
让我们看看这个错误:
警告:在
上面说什么?它说我们试图使用字符串'port'
作为字符串的偏移量。像这样:
$a_string = "string";
// This is ok:
echo $a_string[0]; // s
echo $a_string[1]; // t
echo $a_string[2]; // r
// ...
// !! Not good:
echo $a_string['port'];
// !! Warning: Illegal string offset 'port' in ...
这是什么原因?
出于某种原因,您需要一个数组
,但您有一个字符串
。只是搞错了。也许你的变量被改变了,也许它从来不是一个数组,这真的不重要
可以做些什么?
如果我们知道应该有一个数组
,我们应该做一些基本的调试来确定为什么没有数组
。如果我们不知道是否会有一个数组
或字符串
,事情就会变得有点棘手
我们可以做的是进行各种检查,以确保我们没有关于和或的通知、警告或错误:
和之间有一些微妙的区别。例如,如果$array['key']
的值为null
,isset
返回false
array\u key\u exists
只需检查key是否存在您试图访问字符串
,就好像它是一个数组,并且密钥是字符串
<代码>字符串
无法理解这一点。在代码中,我们可以看到问题:
"hello"["hello"];
// PHP Warning: Illegal string offset 'hello' in php shell code on line 1
"hello"[0];
// No errors.
array("hello" => "val")["hello"];
// No errors. This is *probably* what you wanted.
深入
让我们看看这个错误:
警告:在
上面说什么?它说我们试图使用字符串'port'
作为字符串的偏移量。像这样:
$a_string = "string";
// This is ok:
echo $a_string[0]; // s
echo $a_string[1]; // t
echo $a_string[2]; // r
// ...
// !! Not good:
echo $a_string['port'];
// !! Warning: Illegal string offset 'port' in ...
这是什么原因?
出于某种原因,您需要一个数组
,但您有一个字符串
。只是搞错了。也许你的变量被改变了,也许它从来不是一个数组,这真的不重要
可以做些什么?
如果我们知道应该有一个数组
,我们应该做一些基本的调试来确定为什么没有数组
。如果我们不知道是否会有一个数组
或字符串
,事情就会变得有点棘手
我们可以做的是进行各种检查,以确保我们没有关于和或的通知、警告或错误:
和之间有一些微妙的区别。例如,如果$array['key']
的值为null
,isset
返回false
array\u key\u exists
将只检查key是否存在在我的例子中,我将mysql\u fetch\u assoc更改为mysql\u fetch\u array并求解。求解需要3天:-(我的项目的其他版本使用fetch assoc运行。在我的情况下,我将mysql\u fetch\u assoc更改为mysql\u fetch\u数组并求解。求解需要3天:-(我的项目的其他版本是使用fetch assoc运行的。这里有很多很好的答案,但我发现我的问题要简单得多
我试图运行以下命令:
$x['name'] = $j['name'];
我在$x['name']
上遇到了一个非法字符串
错误,因为我没有首先定义数组。因此,在尝试将内容分配给$x[]
之前,我输入了以下代码行:
$x = array();
这很有效。这里有很多很好的答案,但我发现我的问题要简单得多
我试图运行以下命令:
$x['name'] = $j['name'];
我在$x['name']
上遇到了一个非法字符串
错误,因为我没有首先定义数组。因此,在尝试将内容分配给$x[]
之前,我输入了以下代码行:
$x = array();
从PHP 5.4开始,我们需要传递函数所需的相同数据类型值。例如:
$fruit_counts = array('apples'=>2, 'oranges'=>5, 'pears'=>0);
echo $fruit_counts['oranges']; // echoes 5
$fruit_counts = "an unexpected string assignment";
echo $fruit_counts['oranges']; // causes illegal string offset error
function testimonial($id); // This function expects $id as an integer
调用此函数时,如果提供如下字符串值:
$id = $array['id']; // $id is of string type
testimonial($id); // illegal offset warning
由于数据类型不匹配,这将生成非法偏移警告。为了解决此问题,您可以使用:
从PHP 5.4开始,我们需要传递函数期望的相同数据类型值。例如:
$fruit_counts = array('apples'=>2, 'oranges'=>5, 'pears'=>0);
echo $fruit_counts['oranges']; // echoes 5
$fruit_counts = "an unexpected string assignment";
echo $fruit_counts['oranges']; // causes illegal string offset error
function testimonial($id); // This function expects $id as an integer
调用此函数时,如果提供如下字符串值:
$id = $array['id']; // $id is of string type
testimonial($id); // illegal offset warning
由于数据类型不匹配,这将生成非法偏移警告。为了解决此问题,您可以使用:
为了以防万一,我犯了这个错误,因为我忘了t