Three ways to represent strings in PHP

墨辰丷
Release: 2023-03-25 22:58:02
Original
5082 people have browsed it

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;" must be the closing tag. It must be a single line, and the header Write.
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>
Copy after login

Related recommendations:

php long string definition method

How to get the value after special characters in MySQL in a variable-length string (from stackoverflow)

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!

Related labels:
source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!