Home >Backend Development >PHP Problem >What is the difference between double quotes and single quotes in php

What is the difference between double quotes and single quotes in php

青灯夜游
青灯夜游Original
2020-10-30 16:33:572948browse

Difference: PHP will not parse variables in single quotes, but will output the variable names as they are; PHP will parse variables contained in double quotes. Because single quotes do not need to consider the parsing of variables, the parsing speed is faster than double quotes.

What is the difference between double quotes and single quotes in php

Looking at many codes, sometimes single quotes or double quotes are used to contain string content. So what is the difference between double quotes and single quotes in PHP? The following article will introduce it to you. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

The difference between double quotes and single quotes in php

①The escaped characters are different

The escape character (\) can be used in both single quotes and double quotes, but only the single quotes and escape characters enclosed in single quotes can be escaped. If you enclose a string in double quotes (""), PHP understands more escape sequences for special strings. [Recommended: "PHP Video Tutorial"]

 <?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;;
?>

②Different parsing of variables

Variables appearing in single quote strings will not Replaced by variable value. That is, PHP will not parse variables in single quotes, but will 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;
?>

③The parsing speed is different

Single quotes do not need to consider the parsing of variables and are faster than double quotes.

Supplement:

Another use of PHP quotation marks is that sometimes you need to use PHP to generate text files. The newline character n needs to be double quotation marks to work well. Single Quotation marks will directly output n as a character.

Usage summary:

When there is no need to add variables or single quotes (') and backslashes (\) to the string, try to use single quotes Quoting strings eliminates the need for double-quote checking, escaping, and parsing variables. When including variables, use double quotes to simplify operations. Use single quotes if possible, and wrap them in braces in complex cases.

Related recommendations: php training

The above is the detailed content of What is the difference between double quotes and single quotes in php. 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