PHP 回显 block 行为

标签 php html mysql echo

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 时,我遇到了两件事,但我有点难以解释。

  1. echo block 的任何缩进都会导致网页崩溃。

  2. 如果我将 fetch_array 类型更改为 MYSQLI_ASSOCMYSQLI_BOTH,关联调用如 $row[ 'author'] 导致页面崩溃。而在访问 $row 时使用单行调用 echo 工作正常。

最佳答案

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

  1. 在 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/

相关文章:

java - 从 MySql 表中获取的 Android 应用程序数据

php - 如果不是来自给定的居住国家/地区,如何拒绝 Paypal 付款?

php - 使变量不可变

php - 将另一个表的数据添加到另一个表的循环中

android - 总是在android中下拉菜单

javascript - 尽管存在 JS 错误,Phantomjs 仍需要页面源

javascript - 当我只上传一次时,图像在表格中插入了两次

php - array_push 字符串,来自用于 sql 插入的 json_encode

php - Paypal PHP Rest API用于信用卡支付,如何处理错误以显示给用户

php - 有关表单和 php 的问题(清除表单、实时刷新、多行)