PHP - 警告:非法字符串偏移和奇怪的输出

时间:2021-09-27 15:46:03

I was just playing with PHP array

我刚刚玩PHP数组

$arr = array(); 
$arr['a'] = 'hello';
$arr['a']['b'] = 'world';
var_dump($arr);

This code gives the following error in PHP 5.5.15

此代码在PHP 5.5.15中给出以下错误

Warning: Illegal string offset 'b'

I can guess the reason. (As $arr['a'] is not an array). Thats fine.

我可以猜出原因。 (因为$ arr ['a']不是数组)。没关系。

But i am confused by the output,

但我对输出感到困惑,

array (size=1)
  'a' => string 'wello' (length=5) 

Where is this wello coming from ?

这个欢迎来自哪里?

3 个解决方案

#1


3  

This is because

这是因为

(int)'b' === 0

'b' is silently casted to integer (after the warning was printed) and $arr['a'][0] sets the first symbol of string to what it could—to the first byte of given string, which is apparently 'w'.

'b'被静默地转换为整数(在打印警告之后)并且$ arr ['a'] [0]将字符串的第一个符号设置为它能够到达给定字符串的第一个字节,这显然是'w' ”。

#2


3  

$arr['a'] already exists as a string

$ arr ['a']已作为字符串存在

$arr['a']['b'] is therefore referencing offset b in a string

因此,$ arr ['a'] ['b']引用字符串中的偏移量b

Characters in Strings can be referenced using array-style syntax as though they are an array of characters, so $arr['a']['b'] is trying to reference a character in the string at $arr['a']

字符串中的字符可以使用数组样式语法引用,就像它们是一个字符数组一样,所以$ arr ['a'] ['b']试图在$ arr ['a']中引用字符串中的字符

Offsets in a string are numeric; b is not numeric, hence the warning

字符串中的偏移量是数字; b不是数字,因此警告

PHP is forgiving, and will cast b to a numeric (0) so that it can access a character in the string

PHP是宽容的,并将b转换为数字(0),以便它可以访问字符串中的字符

So character 0 in the string becomes the first character of the value you assigned (world)

所以字符串中的字符0成为您指定的值的第一个字符(世界)

So hello becomes wello

所以你好成为了

#3


2  

This is pretty simple! You can access the string in your array as an array so:

这很简单!您可以将数组中的字符串作为数组访问,以便:

echo $arr["a"][0]; //Output: h

So with the index 'b' it's just get's casted to a int, so it takes 0 and overwrites it with 'w'

因此,对于索引'b',它只是被转换为int,因此它需要0并用'w'覆盖它

You even see this example in the manual: http://php.net/manual/en/language.types.type-juggling.php

您甚至可以在手册中看到这个示例:http://php.net/manual/en/language.types.type-juggling.php

And a quote from there:

从那里引用:

<?php
$a    = 'car'; // $a is a string
$a[0] = 'b';   // $a is still a string
echo $a;       // bar
?>

#1


3  

This is because

这是因为

(int)'b' === 0

'b' is silently casted to integer (after the warning was printed) and $arr['a'][0] sets the first symbol of string to what it could—to the first byte of given string, which is apparently 'w'.

'b'被静默地转换为整数(在打印警告之后)并且$ arr ['a'] [0]将字符串的第一个符号设置为它能够到达给定字符串的第一个字节,这显然是'w' ”。

#2


3  

$arr['a'] already exists as a string

$ arr ['a']已作为字符串存在

$arr['a']['b'] is therefore referencing offset b in a string

因此,$ arr ['a'] ['b']引用字符串中的偏移量b

Characters in Strings can be referenced using array-style syntax as though they are an array of characters, so $arr['a']['b'] is trying to reference a character in the string at $arr['a']

字符串中的字符可以使用数组样式语法引用,就像它们是一个字符数组一样,所以$ arr ['a'] ['b']试图在$ arr ['a']中引用字符串中的字符

Offsets in a string are numeric; b is not numeric, hence the warning

字符串中的偏移量是数字; b不是数字,因此警告

PHP is forgiving, and will cast b to a numeric (0) so that it can access a character in the string

PHP是宽容的,并将b转换为数字(0),以便它可以访问字符串中的字符

So character 0 in the string becomes the first character of the value you assigned (world)

所以字符串中的字符0成为您指定的值的第一个字符(世界)

So hello becomes wello

所以你好成为了

#3


2  

This is pretty simple! You can access the string in your array as an array so:

这很简单!您可以将数组中的字符串作为数组访问,以便:

echo $arr["a"][0]; //Output: h

So with the index 'b' it's just get's casted to a int, so it takes 0 and overwrites it with 'w'

因此,对于索引'b',它只是被转换为int,因此它需要0并用'w'覆盖它

You even see this example in the manual: http://php.net/manual/en/language.types.type-juggling.php

您甚至可以在手册中看到这个示例:http://php.net/manual/en/language.types.type-juggling.php

And a quote from there:

从那里引用:

<?php
$a    = 'car'; // $a is a string
$a[0] = 'b';   // $a is still a string
echo $a;       // bar
?>