'out of range offset' 是什么意思,什么时候 'only the first character of an assigned string is used'?
What does mean by 'out of range offset' and when does 'only the first character of an assigned string is used'?
我正在使用 PHP 7.2.2
我无法理解摘自 PHP Manual
的以下段落
Warning Writing to an out of range offset pads the string with spaces.
Non-integer types are converted to integer. Illegal offset type emits
E_NOTICE. Only the first character of an assigned string is used. As
of PHP 7.1.0, assigning an empty string throws a fatal error.
Formerly, it assigned a NULL byte.
关于上述段落,我的想法是doubts/questions:
- 这里的 'out of range offset' 到底是什么意思?
- 其非整数类型转换为整数。它是正在考虑的字符串中的偏移量还是字符,其类型转换将要发生?
- 'Illegal offset type' 的确切含义是什么?
- 什么时候'only the first character of an assigned string is used'?
- 最后一句'Formerly, it assigned a NULL byte.'bye是什么意思? NULL byte具体是什么意思?
有人可以用易于理解的语言和合适的工作代码示例回答我所有的 doubts/questions 吗?
这里有一个字符串,例如:$string = 'word';
在PHP中,字符串是字节数组。您可以通过数字索引引用字符串中的特定字符(字节)。如果偏移量在 0
(第一个字符)和 strlen($string) - 1
(最后一个字符)之间,则偏移量在范围内。
此示例使用索引 0 到 3 迭代字符串中的字符。
$len = strlen($string);
for ($i=0; $i < $len; $i++) {
$char = $string[$i];
var_dump($i, $char);
}
超出范围的偏移量是任何超出 strlen - 1 的索引。此代码将生成通知:
$char = $string[4];
var_dump($char); // string ''
Notice: Uninitialized string offset: 4
使用 non-integer 类型作为索引会将类型转换为整数。本例中的字符串foo
(非法偏移类型)将被转换为0
,因此字符串的第一个字符将被替换。
$string['foo'] = 'c';
var_dump($string); // string 'cord'
如果您尝试分配一个包含多个字符的字符串,则只会使用它的第一个字符,如果您尝试分配给超出单词末尾的索引,则间隙索引将用空格填充.
$string[5] = 'something';
var_dump($string); // string 'cord s'
手册的这一部分涉及将字符串视为字符数组。
"out of range" offset 表示一个整数索引,其位置比当前字符串本身长,例如$x = "foo"; $x[10] = 'o';
导致 $x
变成 foo o
如果使用 non-integer 索引值,则在访问字符串的索引之前将索引值转换为整数,例如$x = "foo"; $y = $x[true];
导致 $y
取值 $x[1]
- o
非法偏移类型是指通常不能用作数组偏移的任何类型,例如class Foo() {}
- 使用 $x[new Foo()];
索引字符串会引发警告
第一个字符片段意味着如果您尝试将字符串分配给现有字符串的索引,则只会使用分配字符串的第一个字符,例如$x = "foo"; $x[0] = "hi";
导致 $x
变成 hoo
;
在索引处为字符串分配空字符串值现在会导致错误,而不是分配 "null" 字节 [=22=]
,例如$x[0] = ''
会致命的。
我正在使用 PHP 7.2.2
我无法理解摘自 PHP Manual
的以下段落Warning Writing to an out of range offset pads the string with spaces. Non-integer types are converted to integer. Illegal offset type emits E_NOTICE. Only the first character of an assigned string is used. As of PHP 7.1.0, assigning an empty string throws a fatal error. Formerly, it assigned a NULL byte.
关于上述段落,我的想法是doubts/questions:
- 这里的 'out of range offset' 到底是什么意思?
- 其非整数类型转换为整数。它是正在考虑的字符串中的偏移量还是字符,其类型转换将要发生?
- 'Illegal offset type' 的确切含义是什么?
- 什么时候'only the first character of an assigned string is used'?
- 最后一句'Formerly, it assigned a NULL byte.'bye是什么意思? NULL byte具体是什么意思?
有人可以用易于理解的语言和合适的工作代码示例回答我所有的 doubts/questions 吗?
这里有一个字符串,例如:$string = 'word';
在PHP中,字符串是字节数组。您可以通过数字索引引用字符串中的特定字符(字节)。如果偏移量在 0
(第一个字符)和 strlen($string) - 1
(最后一个字符)之间,则偏移量在范围内。
此示例使用索引 0 到 3 迭代字符串中的字符。
$len = strlen($string);
for ($i=0; $i < $len; $i++) {
$char = $string[$i];
var_dump($i, $char);
}
超出范围的偏移量是任何超出 strlen - 1 的索引。此代码将生成通知:
$char = $string[4];
var_dump($char); // string ''
Notice: Uninitialized string offset: 4
使用 non-integer 类型作为索引会将类型转换为整数。本例中的字符串foo
(非法偏移类型)将被转换为0
,因此字符串的第一个字符将被替换。
$string['foo'] = 'c';
var_dump($string); // string 'cord'
如果您尝试分配一个包含多个字符的字符串,则只会使用它的第一个字符,如果您尝试分配给超出单词末尾的索引,则间隙索引将用空格填充.
$string[5] = 'something';
var_dump($string); // string 'cord s'
手册的这一部分涉及将字符串视为字符数组。
"out of range" offset 表示一个整数索引,其位置比当前字符串本身长,例如
$x = "foo"; $x[10] = 'o';
导致$x
变成foo o
如果使用 non-integer 索引值,则在访问字符串的索引之前将索引值转换为整数,例如
$x = "foo"; $y = $x[true];
导致$y
取值$x[1]
-o
非法偏移类型是指通常不能用作数组偏移的任何类型,例如
class Foo() {}
- 使用$x[new Foo()];
索引字符串会引发警告第一个字符片段意味着如果您尝试将字符串分配给现有字符串的索引,则只会使用分配字符串的第一个字符,例如
$x = "foo"; $x[0] = "hi";
导致$x
变成hoo
;在索引处为字符串分配空字符串值现在会导致错误,而不是分配 "null" 字节
[=22=]
,例如$x[0] = ''
会致命的。