Home > Backend Development > PHP Problem > What are the differences between single quotes and double quotes in php

What are the differences between single quotes and double quotes in php

王林
Release: 2023-03-01 18:00:01
Original
8027 people have browsed it

The difference between single quotes and double quotes in php is: 1. Different escaped characters; 2. Different parsing of variables; 3. Different parsing speeds. PHP will not parse variables in single quotes, but will output the variable names as they are; PHP can parse variables contained in double quotes.

What are the differences between single quotes and double quotes in php

1. The escaped characters are different

Escape characters can be used in both single quotes and double quotes ( \), but only single quotes enclosed in single quotes and the escape character itself can be escaped. If you enclose a string in double quotes (""), PHP knows more about special string escape sequences.

<?php
$str1 = &#39;\&#39;,\\,\r\n\t\v\$\"&#39;;
echo $str1,&#39;<br />&#39;;  
$str2 = "\",\\,a\r\n\tb\v\$\&#39;";
echo $str2,&#39;<br />&#39;;
?>
Copy after login

2. Different parsing of variables

Variables appearing in single quotation mark strings will not be replaced by variable values, that is, PHP will not parse the variables in single quotation marks. variable, but output the variable name as is. The most important thing about double-quoted strings is that the variable names in them will be replaced by variable values, that is, variables contained in double quotes can be parsed.

<?php
$age = 20;
$str1 = &#39;I am $age years old&#39;;
$str2 = "I am $age years old";
echo $str1,&#39;<br />&#39;; // I am $age years old 
echo $str2,&#39;<br />&#39;; // I am 20 years old;
?>
Copy after login

3. Different parsing speeds

Single quotes do not need to consider the parsing of variables, so they are faster than double quotes. But sometimes double quotes are easier to use, such as when piecing together sql statements.

//使用单引号
echo &#39; this \n is \r the blog \t of \\ zhoumanhe \\&#39;; 
//上面使用单引号输出的值是 this \n is \r the blog \t of \ zhoumanhe \  
echo &#39;&#39;;
echo "";  
//使用双引号
echo "this \n is \r the blog \t of \\ zhoumanhe \\"; 
//上面使用双引号输出的值是 this is the blog of \ zhoumanhe \
Copy after login

If you want to know more related knowledge, please visit php中文网.

The above is the detailed content of What are the differences between single quotes and double quotes 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