Home > Article > Backend Development > Three ways to represent strings in PHP
This article mainly introduces the three representation methods of strings in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.
(1) Use single quotes
The escape characters within single quotes can only be: \\, \'
(2) Use double quotes
Within double quotes, all Escape characters can be used. Such as: \\, \', \", \$, \n, \r, \t
(3) Representation method of long string
Long string representation must be placed in "<<
heredoc can customize the name.
Can directly parse PHP variables.
Notes on strings
1. Within double quotes, the output is the value of the variable. For example: echo "{$name}'s age is 24 years old"
2. Within single quotes, the name of the variable is output, not the value.
3. Within double quotes, if a variable name is followed by A non-empty character will confuse the compiler. The solution to this situation is: it can be followed by a space or punctuation marks (special characters) in English, which will parse the variable. The common method is to add a Braces, {$name}
<!DOCTYPE HTML> <HTML> <HEAD> <TITLE> New Document </TITLE> <META name="Generator" content="EditPlus"> <META name="Author" content=""> <META name="Keywords" content=""> <META name="Description" content=""> <style type="text/css"> </style> <script type="text/javascript"> </script> </HEAD> <BODY> <?php $name="张三"; $age=24; $str=<<<heredoc 这是一个很长很长很长的字符串,可以有HTML,js,css代码,但是不能有php代码。可以读取php变量的值,但不能作运算<br> {$name}的年龄是:{$age} heredoc; //heredoc 必须顶头写,且单独一行 echo $str; ?> </BODY> </HTML>
Related recommendations:
php long string definition method
An example of php long string (here document) definition method
The above is the detailed content of Three ways to represent strings in PHP. For more information, please follow other related articles on the PHP Chinese website!