Home > Article > Backend Development > What is the difference between single quotes and double quotes in php
phpThe difference between single quotes and double quotes: 1. The escape character [\] can be used in both single quotes and double quotes, but only single quotes and escapes enclosed in single quotes can be escaped. The escape character itself; 2. Variables appearing in single-quoted strings will not be replaced by variable values, while double-quoted variable names will be replaced by variable values.
The difference between single quotes and double quotes:
1. The escaped characters are different
The escape character (\) can be used in both single quotes and double quotes, but only single quotes and escape characters enclosed in single quotes 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 quote strings will not be replaced by variable values. 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; ?>
3. Different parsing speeds
Single quotes do not need to consider the parsing of variables and are faster than double quotes. It is recommended to use single quotes. Sometimes double quotes are also It is easier to use, such as when piecing together sql statements
Backslash
//使用单引号 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 \
using sql
Assume that constants are used in the query conditions, for example:
select * from abc_table where user_name='abc';
SQL statement can be written as:
SQLstr = “select * from abc_table where user _name= ‘abc'” ;
Assuming that variables are used in the query conditions, for example:
$user_name = $_REQUEST['user_name']; //字符串变量
or
$user=array (”name”=> $_REQUEST['user_name‘,"age"=>$_REQUEST['age'];//数组变量
SQL statement can be written as:
SQLstr = “select * from abc_table where user_name = ‘ ” . $user_name . ” ‘ “; SQLstr = “select * from abc_table where user_name = ‘ ” . $user["name"] . ” ‘ “;
Compare:
SQLstr=”select * from abc_table where user_name = ‘ abc ‘ ” ; SQLstr=”select * from abc_table where user_name =' ” . $user _name . ” ‘ “; SQLstr=”select * from abc_table where user_name =' ” . $user["name"] . ” ‘ “;
SQLstr can be decomposed into the following three parts:
1:”select * from table where user_name = ‘ ” //固定SQL语句 2:$user //变量 3:” ‘ ”
Attachment: You have also seen that the tags in echo '076402276aae5dbec7f672f8f4e5cc81'; html are in single Valid within both quotes and double quotes.
Summarize the principles of using quotation marks in PHP
1. Use quotation marks for string values
2. Try to use single quotation marks in PHP, and all HTML codes Use double quotes
3. When including variables, use double quotes to simplify the operation
4. In complex situations, use curly braces
PHP quotes and One use is that sometimes you need to use PHP to generate a text file. The newline character n needs to be double quoted. Single quotes 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 to quote the string, because double quote checking and escaping are omitted. and the time above the parsed variable. Use single quotes whenever possible.
If you want to learn more about programming, please pay attention to the php training column!
The above is the detailed content of What is the difference between single quotes and double quotes in php. For more information, please follow other related articles on the PHP Chinese website!