Home > php教程 > php手册 > body text

PHP EOF(heredoc)的使用,eofheredoc

WBOY
Release: 2016-06-13 09:05:57
Original
735 people have browsed it

PHP EOF(heredoc)的使用,eofheredoc

<?php
/*
Heredoc技术,在PHP手册和技术书籍中一般没有详细讲述,只是提到了这是一种Perl风格的字符串输出技术。
目前一些论坛程序和CMS系统使用了这种技术,前不久看一个朋友的PHPWIND论坛时碰到过使用这种技术的。
如果你需要在PHP中写大块的HTML拼装操作,这不失为是一种很好的选择,因为你不需要担心引号带的问题,
也省去了字符串连接的繁琐。
*/
//如下例子:
$var = 'PHP变量';
echo <<<EOF
    <div>
        PHP heredoc技术
        <br />
        $var
    </div>
EOF;

echo <<<zhangshan
    <hr />
    <div>
        PHP heredoc技术
        <br />
        $var
    </div>
zhangshan;


/*
注意:

1.herdoc 以&ldquo;<<<End&rdquo;开始标记开始,以&ldquo;End;&rdquo;结束标记结束,结束标记必须单独占一行、
  前面不能有任何字符串包括空格,结尾要有分号结束。

2.结束开始标记必须相同,比如常用大写的EOT、EOD、EOF来表示,不是必须使用这3个标记,
  和PHP的变量名规则基本相同,一般使用约定都是大写。

3.开始和结束标记之间的PHP变量会被解析,函数不会执行、不能进行运算操作。
  变量之间的连接不需要字符串连接符。

4.标记字符串不能在开始结束标记之间出现一样的字符串,区分大小写。
*/

//例如下面:
$a = 12;
$b = 12;
$arr = array('hello'=>'world');
echo <<<EOF
    <div>
        PHP heredoc技术
        <br />
        $a$b
        <br />
        $a+$b
        <br />
        {$arr['hello']}
    </div>
EOF;
Copy after login

  

Related labels:
php
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!