Home > Backend Development > PHP Tutorial > PHP delimiter EOF explanation

PHP delimiter EOF explanation

不言
Release: 2023-03-24 17:30:01
Original
4736 people have browsed it

The content of this article is about the explanation of the PHP delimiter EOF. It has a certain reference value. Now I share it with you. Friends in need can refer to

Heredoc technology. It can be used to output large sections of html and javascript scripts

1. The function of the PHP delimiter is to output the things inside it as it is, including line breaks and other formats;
2. Delimited in PHP Any special characters in the delimiter do not need to be escaped;
3. PHP variables in the PHP delimiter will be replaced with their values ​​normally.
As follows:

<?php
    $name = &#39;浅水游&#39;;    //下面<<<EOT后面不能有空格
    print <<<EOT
            <html>
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
            <title>Untitled Document</title>
            </head>
            <body>
            <!--12321-->
            Hello,{$name}!
            Hello,$name!
            </body>
            </html>
EOT; //注意末尾的结束符必须靠边,其前面不能有空格?>
Copy after login
Copy after login
<?php
$out = 
<<<EOF
    <a href="javascript:edit(&#39;asd&#39;, &#39;aaa&#39;)">编辑</a> |

    <font color="#ccc">删除</font>

    <a href="javascript:confirmurl(&#39;?m=admin&posid=12&#39;)">删除</a> |

    <font color="red">启用</font></a> |  

    <a href="javascript:preview(&#39;3&#39;,&#39;ds&#39;)"><font color="green">演示</font></a>
EOF;echo $out;?>
Copy after login

Note:

1.以<<<End开始标记开始,以End结束标记结束,**结束标记必须顶头写**,不能有缩进和空格,且在结束标记末尾要有分号 。2.开始标记和开始标记相同,比如常用大写的EOT、EOD、EOF来表示,但是不只限于那几个,只要保证开始标记和结束标记不在正文中出现即可。3.位于开始标记和结束标记之间的变量可以被正常解析,但是函数则不可以。在heredoc中,变量不需要用连接符.或,来拼接,如下:$v=2;$a= <<<EOF"abc"$v"123"EOF;echo $a; //结果连同双引号一起输出:"abc"2 "123"
Copy after login
Copy after login
4.heredoc常用在输出包含大量HTML语法d文档的时候。比如:函数outputhtml()要输出HTML的主页。可以有两种写法。很明显第二种写法比较简单和易于阅读。
第一种

function outputhtml(){
    echo "<html>";
    echo "<head><title>主页</title></head>";
    echo "<body>主页内容</body>";
    echo "</html>;
}
第二种
function outputhtml()
{
    echo <<<EOT
    <html>
    <head><title>主页</title></head>
    <body>主页内容</body>
    </html>EOT;
}
outputhtml();
Copy after login

         

Heredoc technology. It can be used to output large sections of html and javascript scripts

1. The function of the PHP delimiter is to output the things inside it as it is, including line breaks and other formats;
2. Delimited in PHP Any special characters in the delimiter do not need to be escaped;
3. PHP variables in the PHP delimiter will be replaced with their values ​​normally.
As follows:

<?php
    $name = &#39;浅水游&#39;;    //下面<<<EOT后面不能有空格
    print <<<EOT
            <html>
            <head>
            <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
            <title>Untitled Document</title>
            </head>
            <body>
            <!--12321-->
            Hello,{$name}!
            Hello,$name!
            </body>
            </html>
EOT; //注意末尾的结束符必须靠边,其前面不能有空格?>
Copy after login
Copy after login
<?php
$out = 
<<<EOF
    <a href="javascript:edit(&#39;asd&#39;, &#39;aaa&#39;)">编辑</a> |

    <font color="#ccc">删除</font>

    <a href="javascript:confirmurl(&#39;?m=admin&posid=12&#39;)">删除</a> |

    <font color="red">启用</font></a> |  

    <a href="javascript:preview(&#39;3&#39;,&#39;ds&#39;)"><font color="green">演示</font></a>
EOF;echo $out;?>
Copy after login

Note:

1.以<<<End开始标记开始,以End结束标记结束,**结束标记必须顶头写**,不能有缩进和空格,且在结束标记末尾要有分号 。2.开始标记和开始标记相同,比如常用大写的EOT、EOD、EOF来表示,但是不只限于那几个,只要保证开始标记和结束标记不在正文中出现即可。3.位于开始标记和结束标记之间的变量可以被正常解析,但是函数则不可以。在heredoc中,变量不需要用连接符.或,来拼接,如下:$v=2;$a= <<<EOF"abc"$v"123"EOF;echo $a; //结果连同双引号一起输出:"abc"2 "123"
Copy after login
Copy after login
4.heredoc常用在输出包含大量HTML语法d文档的时候。比如:函数outputhtml()要输出HTML的主页。可以有两种写法。很明显第二种写法比较简单和易于阅读。
第一种

function outputhtml(){
    echo "<html>";
    echo "<head><title>主页</title></head>";
    echo "<body>主页内容</body>";
    echo "</html>;
}
第二种
function outputhtml()
{
    echo <<<EOT
    <html>
    <head><title>主页</title></head>
    <body>主页内容</body>
    </html>EOT;
}
outputhtml();
Copy after login

Related recommendations:

How to use the PHP delimiter eof

The above is the detailed content of PHP delimiter EOF explanation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template