PHP+Tp5中heredoc和nowdoc解析

来源:互联网 发布:指南针软件怎么使用 编辑:程序博客网 时间:2024/05/17 16:11

普通转换模式

代码实例

<?phpnamespace app\index\controller;class Index{    public function index(){        header('content-type:text/html;charset=utf-8');        $table= "<table border='1' width=\"80%\">//注意这里的双引号                    <tr>                        <td>编号</td>                    </tr>                </table>";        echo $table;               }}

运行结果

这里写图片描述

heredoc

语法结构

<<<标识名称
内容
标识名称;

标识名只能包含字母数字下划线,并且必须为字母和下划线开始
结束符前不能有任何内容,像制表符和空格都不可以
PHP5.3之后可以将标志符用双引号扩起来,其它和双引号作用一样

代码实例

<?phpnamespace app\index\controller;class Index{    public function index(){        header('content-type:text/html;charset=utf-8');        $table= <<<EOF                <table border='1' width="80%">                    <tr>                        <td>编号</td>                    </tr>                </table> EOF;//注意这里前面不能有任何的东西        echo $table;               }}

运行结果

跟第一种是一样的

nowdoc

语法结构

<<<’标识名称’
内容。。。
标识名称;

注意nowdoc和单引号作用一样,不解析变量和转衣服,标识名称需要放在单引号中

代码实例

<?phpnamespace app\index\controller;class Index{    public function index(){        header('content-type:text/html;charset=utf-8');        $username = 'tuzi';        $str = <<<'EOD'                hello {$username}EOD;        echo $str;//注意username会不解析     }}

运行结果

hello {$username}