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

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

不言
Release: 2023-04-04 12:10:02
forward
5452 people have browsed it

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;
// 结束需要独立一行且前后不能空格
?>
Copy after login

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;
?>
Copy after login

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!

Related labels:
eof
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