for($j = 0; $j < $rows; ++$j)
{
$result->data_seek($j);
$row = $result->fetch_array(MYSQLI_NUM);
echo <<<_END
<pre>
Author: $row[0]
Title: $row[1]
Type: $row[2]
Year: $row[3]
ISBN: $row[4]
</pre>
_END;
}
在对文本 block 使用 echo
时,我遇到了两件事,但我有点难以解释。
echo
block 的任何缩进都会导致网页崩溃。如果我将
fetch_array
类型更改为MYSQLI_ASSOC
或MYSQLI_BOTH
,关联调用如$row[ 'author']
导致页面崩溃。而在访问$row
时使用单行调用echo
工作正常。
最佳答案
- PHP heredoc的结束标识符不应缩进。这是手册中的警告段落。
Warning It is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system.
在 heredoc 中,数组键不应该被引用,或者在
${}
符号中被引用。$array[key] // works (the only place you do not quote string keys in PHP) ${array["key"]} // works $array["key"] // doesn't work
关于PHP 回显 block 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32314291/