Home  >  Article  >  Backend Development  >  Introduction to the usage of EOF in PHP (with examples)

Introduction to the usage of EOF in PHP (with examples)

不言
不言forward
2018-11-14 17:03:265333browse

This article brings you an introduction to the usage of EOF in PHP (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

PHP EOF (heredoc) is a way to define a string in command line shells (such as sh, csh, ksh, bash, PowerShell and zsh) and programming languages ​​(Perl, PHP, Python and Ruby) method.

Usage overview:

must be followed by a semicolon, otherwise the compilation will not pass;

EOF can be replaced by any other character, as long as the end identifier and the start identifier are consistent;

The end mark must occupy a line by itself at the top of the line (that is, it must start from the beginning of the line, and cannot be connected with any blanks or characters);

The start mark can be without quotation marks or with single and double quotation marks, without Quotes have the same effect as with quotes, interpreting embedded variables and escape characters, while single quotes do not interpret embedded variables and escape characters;

When the content requires embedded guidance (single quotes or double quotes When using quotation marks), there is no need to add escape symbols, single and double quotation marks will be escaped by itself;

<?php
echo <<<EOF
    <h1>我的第一个标题</h1>
    <p>我的第一个段落。</p>
EOF;
// 结束需要独立一行且前后不能空格
?>

Note:

starts with <<

The start tag and the end tag are the same, such as commonly used capital EOT, EOD, EOF to express, but not limited to those few (can also use: JSON, HTML, etc.), as long as the start tag and the end tag are not Just appear in the text.

Variables located between the start tag and the end tag can be parsed normally, but functions cannot. In heredoc, variables do not need to be spliced ​​with connectors . or , as follows:

<?php
$name="如约智惠";
$a= <<<EOF
    "abc"$name
    "123"
EOF;
// 结束需要独立一行且前后不能空格
echo $a;
?>

The above is the detailed content of Introduction to the usage of EOF in PHP (with examples). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete
Previous article:What is a php career?Next article:What is a php career?