Home >Backend Development >PHP Problem >What is the difference between double quotes and single quotes in php
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.
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 = '\',\\,\r\n\t\v\$\"'; echo $str1,'<br />'; $str2 = "\",\\,a\r\n\tb\v\$\'"; echo $str2,'<br />'; ?>
②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 = 'I am $age years old'; $str2 = "I am $age years old"; echo $str1,'<br />'; // I am $age years old echo $str2,'<br />'; // 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!