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.
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 = '\',\\,\r\n\t\v\$\"'; echo $str1,'<br />'; $str2 = "\",\\,a\r\n\tb\v\$\'"; echo $str2,'<br />'; ?>
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 = '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; ?>
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 ' this \n is \r the blog \t of \\ zhoumanhe \\'; //上面使用单引号输出的值是 this \n is \r the blog \t of \ zhoumanhe \ echo ''; echo ""; //使用双引号 echo "this \n is \r the blog \t of \\ zhoumanhe \\"; //上面使用双引号输出的值是 this is the blog of \ zhoumanhe \
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!