How to use Heredoc and Nowdoc in PHP

Source: Internet
Author: User
Tags closing tag opening and closing tags

I. Structure and usage of Heredoc

The HEREDOC structure is like a double-quote string that does not use double quotes, which means that single quotes are not escaped in the Heredoc structure. The variables in their structure will be replaced, but be careful when they contain complex variables in the HEREDOC structure. It is useful for formatting output content. Specifically, it has the following characteristics:

1, opening and closing tags use the same string, usually written in uppercase letters.

2. No spaces or extra characters can appear after the start tag.

3. The closing tag must be written on the head, not indented and blank, and there should be a semicolon at the end of the tag.

4. Variables located between the opening and closing tags can be parsed normally, but the function is not allowed. In Heredoc, a variable does not need a connector. Or, to splice.

Such as:

12345678910111213 <?phpfunctionoutputhtml(){ //自 PHP 5.3.0 起还可以在 Heredoc 结构中用双引号来声明标识符,所以开头这句也可以写为echo <<<"EOT"echo<<<EOT         <body>主页内容</body>   EOT;}outputhtml();?>

Here, you don't have to look like the normal PHP notation, echo "

Give an example of a variable reference:

123456789101112131415 <?php$name‘1ss‘;print<<<EOT<meta http-equiv="Content-Type"content="text/html; charset=gb2312"/><title>Untitled Document</title><body><!--12321-->Hello,$name!</body>EOT;?>

II. structure and usage of Nowdoc

The NOWDOC structure is added in PHP 5.3.0 and later versions, with the same usage and heredoc, unlike the NOWDOC structure, which is similar to the single-quote string. Parse operation is not performed in Nowdoc. This structure is ideal for embedding PHP code or other large pieces of text without escaping special characters. The [cdata[]] structure of SGML is similar to declaring a large segment of text without parsing, and the NOWDOC structure has the same characteristics.

A NOWDOC structure is also marked with the same <<< as the HEREDOCS structure, but the following identifiers are enclosed in single quotes, i.e. <<< ' EOT '.

Example: Examples of complex arguments in now structures

12345678910111213141516171819202122232425 <?php$str= <<<‘EOD‘Example of stringspanning multiple linesusing nowdoc syntax.EOD;/* 含有变量的更复杂的示例 */classfoo{    public$foo;    public$bar;    function foo()    {        $this->foo = ‘Foo‘;        $this->bar = array(‘Bar1‘‘Bar2‘‘Bar3‘);    }}$foonewfoo();$name‘MyName‘;echo<<<‘EOT‘My name is "$name". I am printing some $foo->foo.Now, I am printing some {$foo->bar[1]}.This should not printa capital ‘A‘: x41EOT;?>

Its output is:

123 My name is "$name". I am printing some $foo->foo.Now, I am printing some {$foo->bar[1]}.This should not print a capital ‘A‘: x41

The specific can be compared with Heredoc, in the Heredoc, the variable will be parsed normally. X41 will also be parsed also a.

Third, other

When using Heredoc and Nowdoc, you will often encounter the following error:

1 Parse error: syntax error, unexpected T_SL in php

The problem is caused by a space after the delimiter. If there is a space behind the EOT in the previous example, the error will be reported. There is a small trick to working with spaces. With Vim as an example, the EOF at the beginning and end is highlighted when the syntax is correct. When not highlighted, it proves that there is a problem with the grammar.

How to use Heredoc and Nowdoc in PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.