放牧代码和思想
专注自然语言处理、机器学习算法
    This thing called love. Know I would've. Thrown it all away. Wouldn't hesitate.

PHP的标记符echo<<<theEnd的正确用法

PHP的标记符<<<theEnd有个很有意思的问题,如果你运行下面这段代码

<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<title>测试程序</title>
</head>
<body>
<?php
    echo "你好\n";
    $variable = '啊哈哈';
    echo <<<END
    This uses the "here document" syntax to output
    multiple lines with $variable interpolation. Note
    that the here document terminator must appear on a
    line with just a semicolon. no extra whitespace!
    END;
?>
</body>
</html>

会得到Parse error:  syntax error, unexpected end of file in的错误

原来是因为标记符号必须顶头写,好像所有语言的标记符都是这样的,比如汇编。所以正确的用法是:

<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<title>测试程序</title>
</head>
<body>
<?php
    echo "你好\n";
    $variable = '啊哈哈';
    echo <<<END
    This uses the "here document" syntax to output
    multiple lines with $variable interpolation. Note
    that the here document terminator must appear on a
    line with just a semicolon. no extra whitespace!
END;
?>
</body>
</html>

知识共享许可协议 知识共享署名-非商业性使用-相同方式共享码农场 » PHP的标记符echo<<<theEnd的正确用法

评论 欢迎留言

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

我的作品

HanLP自然语言处理包《自然语言处理入门》