① escaped characters are different
Escape characters (\) can be used in both single and double quotes, but only single quotes and escape escape characters that are enclosed in single quotes are escaped. If you enclose a string in double quotes (""), PHP knows more about the escape sequence of special strings.
The code is as follows |
Copy Code |
<?php $str 1 = ' \ ', \\,\r\n\t\v\$\ '; echo $str 1, ' <br/> ';
$str 2 = "\", \\,a\r\n\tb\v\$\ ' "; echo $str 2, ' <br/> '; ?>
|
② differ in the resolution of variables
variables that appear in single quote strings are not replaced by variable values. That is, PHP does not parse the variable in single quotes, but instead prints the variable name as it is. The most important point of a double quote string is that the variable name is substituted by the variable value, which means that the variable contained in the double quotation marks can be parsed.
The code is as follows |
Copy Code |
<?php $age = 20; $str 1 = ' I am $age years old '; $str 2 = "I am $age years old"; echo $str 1, ' <br/> '; I am $age years old echo $str 2, ' <br/> '; I am years old; ?>
|
③ parsing speed is different
Single quotes do not need to consider the resolution of variables, faster than double quotes. single quotes are recommended. Sometimes double quotes are handy, like piecing together SQL statements
Back slash
The code is as follows |
Copy Code |
Using single quotes Echo ' this\n is\r the blog\t of\\zhoumanhe\\ '; The value above using single quotes is this\n is\r the blog\t of\zhoumanhe\
Echo ' '; echo " ";
Use double quotes echo "this\n is\r the blog\t of\\zhoumanhe\\"; The value above with double quotation marks is the "is" blog of\zhoumanhe\ |
Using SQL
The code is as follows |
Copy Code |
Select * from abc_table where user_name= ' abc '; The SQL statement can be written as: Sqlstr = "SELECT * from abc_table where user _name= ' abc '"; Suppose a variable is used in a query condition, for example: $user _name = $_reque st[' user_name ']; The string variable or $user =array ("name" => $_request[' user_name ', "age" =>$_request[the "age"];//an array variable 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 ']. ” ‘ “; The Sqlstr can be decomposed into the following 3 sections: 1: "SELECT * FROM table where user_name = '"//fixed SQL statement 2: $user//variable 3: "' |
Attachment: We have also seen Echo ' <br/> '; tags in HTML are valid in both single and double quotes.
Summarize the use of the PHP quotes principle
1. The value of the string is enclosed in quotation marks
In 2.PHP, try to use single quotes, all HTML code with double quotes
3. When you include a variable, you can use double quotes to simplify the operation
4. In complex cases, wrap them in curly braces.
Another use of PHP quotes is that sometimes you need to use PHP to generate text files, line feed n need double quotes to work, single quotes will directly n as the character output.
Usage Summary: When you do not need to add a variable or single quotation mark (') and a backslash (\) in a string, try to enclose the string in single quotes as much as possible, because you omit double quotes to check the time that is spent on escaping and parsing variables. can use single quotation marks as much as possible in single quotes.