"assigning an empty string" 在这种情况下是什么意思?

What's the meaning of "assigning an empty string" in this context?

我正在使用 PHP 7.1.0

我在 PHP 手册的 [字符串章节][1]

中看到了以下令人困惑的文字

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.

我努力从上面的文字中理解从句的意思

"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."

上面两个说法中的第一个完全没看懂

对于第二个语句,我试图通过实际代码来理解它。因此,为此我编写了以下代码,但我不确定这段代码是否演示了手册中的声明想要说的内容。以下是我的代码片段。从中,告诉我我是否正确理解了该语句并正确地编写了代码,以证明 PHP 手册中语句的含义。如果我做错了,请相应地更正我的代码以符合 PHP 手册中的声明。

代码段:

<?php 
$rootbeer = 'A & W';
$rootbeer[] = 'N';
?>

PHP7.1.0 中的输出:

Fatal error: Uncaught Error: [] operator not supported for strings in hello.php:3 Stack trace: #0 {main} thrown in hello.php on line 3

PHP5.6.30 中的输出:

Fatal error: [] operator not supported for strings in hello.php on line 3

从上面的两个输出中,我在 PHP 5.6.30 的任何地方都看不到 NULL byte 正在获取分配给某物。与这两个版本一样,我得到几乎相同的输出,相同的错误。

请演示 PHP 7.1.0 和 PHP 5.6.30 中输出之间的差异。这样我就可以更好地了解其中的差异。

您没有分配空字符串,也没有使用超出范围的偏移量。我相信您想尝试的代码如下所示:

<?php
$rootbeer = 'A & W';
$rootbeer[7] = '';
echo $rootbeer;

这将在 PHP 5.6 中正常工作并输出文档中提到的填充字符串。

在PHP 7.1+中,你会得到一个警告

PHP Warning: Cannot assign an empty string to a string offset in yourfile.php on line 3

并且字符串不会被改变。