Home >Backend Development >PHP Tutorial >Heredoc and nowdoc define strings in php_PHP tutorial

Heredoc and nowdoc define strings in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:08:44917browse

[php]

// When you need to write a large paragraph of text, many lines, there are single quotes and double quotes in it, which leads to escaping, it is more troublesome.
// You can also use heredoc and nowdoc to define strings
$age = 29;
$str = <<
Let me write a line,
Come to line 2, ' " ,
dsafd abc t rn haha ​​
$age
cont;
echo $str;
/**
How to write heredoc
<<
Write a large paragraph of text in the middle
Identifier;
Note:
1: The requirements for identifier names and variable names are the same.
2: The heredoc identifier must be on its own line and not preceded by any other characters.
3: heredoc processes text in the same way as double quotes, that is, rnt, variables, etc. can be parsed.
**/
// Heredoc is very convenient for writing large paragraphs of text, but the internal character analysis is the same as double quotes.
// If I don’t want to do this, is there any way to write large text like heredoc?
// However, parsing text is as simple as single quotes.
// Is there any such usage?
// Answer: Yes www.2cto.com
// After 5.3.0, the nowdoc method has been added, which can achieve the above effect.
echo '
';
$str = <<<'cont'
Let me write a line,
Come to line 2, ' " ,
dsafd abc t rn haha ​​
$age
cont;
echo $str;
/***
Nowdoc is written in the same way as heredoc, except that the identifier is wrapped in single quotes,
In this way, the parsing of large text has the same effect as single quotes.
Do not escape n r t, variables, etc.
***/
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477764.htmlTechArticle[php] ?php // When you need to write a large paragraph of text, many lines, and there are single quotes in it, There are double quotes, which makes escaping more troublesome. // You can also use heredoc and nowdoc to define strings...
Statement:
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