Home  >  Article  >  Backend Development  >  Detailed explanation of the differences between the three PHP string delimiters

Detailed explanation of the differences between the three PHP string delimiters

小云云
小云云Original
2018-03-21 14:18:403050browse

This article mainly shares with you the detailed explanation of the differences between the three PHP string delimiters. I hope it can help you.

The difference between single quotes, double quotes, <<< is as follows:

Previous: Today I suddenly encountered <<

1. Single quotes: 'a string'
\' is the only escape code and cannot embed variables (the included variables are not parsed and are just output as they are).

2. Double quotes: ”a $better string\n”
Standard escape codes can be used normally, and variables can be embedded (the included variables will be parsed by PHP).

3. "Here-doc" syntax: PHP delimiter

1 $a=1;  
2 echo <<<EOT  
3 <script language="JavaScript" type="text/JavaScript">  
4 var a = $a;  
5 alert(a);  
6 </script>  
7 EOT;

 <<

Its functions can be summarized into three points:

1. The function of the PHP delimiter is to output everything inside it as it is, including line breaks and other formats;

 2. Any special characters in the PHP delimiter do not need to be escaped and are written as usual;

 3. The PHP variables in the PHP delimiter will be replaced with their values ​​normally (php parse nested variables).

Notes:

1. The character EOT after <<< is self-defined. Anything is acceptable, but the character at the end must be the same. , they appear in pairs.

 2. Start with <<

3. Embedded PHP variables should be enclosed in {} when necessary to tell the PHP parser that this is a PHP variable, such as {$name} to avoid ambiguity. {} is not needed in other cases. .
Note:

Based on the above, it can be seen that the execution efficiency of single quotes is higher than the latter two. Therefore, it should be considered appropriately during use.

Related recommendations:

Mastering the basic knowledge of php - four kinds of delimiters

The above is the detailed content of Detailed explanation of the differences between the three PHP string delimiters. For more information, please follow other related articles on the PHP Chinese website!

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