In PHP, we can use single quotes or double quotation marks to represent strings. However, as developers, we should understand the differences. Single quotation marks and double quotation marks can parse variables for defining characters, and directly output variables. at the same time, single quotation marks and double quotation marks are different in character processing ① escape characters.
You can use escape characters (\) in single quotes and double quotes, but you can only escape single quotes and escape characters cited in single quotes. If double quotation marks ("") are used to enclose strings, PHP understands the escape sequences of more special strings.
<?php$str1 = '\',\\,\r\n\t\v\$\"';echo $str1,'
'; $str2 = "\",\\,a\r\n\tb\v\$\'";echo $str2,'
';?>
② Different variables are parsed.
Variables appearing in single quotes strings are not replaced by variable values. That is, PHP does not parse the variables in single quotes, but outputs the variable name as is. The most important aspect of a double quotation mark string is that the variable name is replaced by the variable value, that is, the variable contained in double quotation marks can be parsed.
<?php$age = 20;$str1 = 'I am $age years old';$str2 = "I am $age years old";echo $str1,'
'; // I am $age years old echo $str2,'
'; // I am 20 years old;?>
③ Different resolution speeds
Variable parsing is not required for single quotes, which is faster than double quotes. we recommend that you use single quotes. sometimes double quotes are also easier to use. for example, you can splice SQL statements
Backslash
// Use single quotes echo 'This \ n is \ r the blog \ t of \ zhoumanhe \\'; // the value output using single quotes above is this \ n is \ r the blog \ t of \ zhoumanhe \ echo ''; echo ""; // use double quotation marks echo "this \ n is \ r the blog \ t of \ zhoumanhe \\"; // the value output using double quotation marks above is this is the blog of \ zhoumanhe \
Use SQL
Assume that constants are used in the query conditions, for example:
select * from abc_table where user_name='abc';
The SQL statement can be written as follows:
SQLstr = “select * from abc_table where user _name= ‘abc'” ;
Assume that the query conditions use variables, for example:
$ User_name = $ _ REQUEST ['User _ name']; // string variable
Or
$ User = array ("name" =>$ _ REQUEST ['User _ name', "age" =>$ _ REQUEST ['age']; // array variable
The SQL statement can be written as follows:
SQLstr = “select * from abc_table where user_name = ‘ ” . $user_name . ” ‘ “;SQLstr = “select * from abc_table where user_name = ‘ ” . $user["name"] . ” ‘ “;
Comparison:
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 divided into the following three parts:
1: "select * from table where user_name = '" // fixed SQL Statement 2: $ user // variable 3 :"'"
Note: You have also seen echo'
'; Tags in html are valid in single quotes and double quotation marks.
To sum up the usage principles of PHP quotes
1. the string value is enclosed in quotation marks.
2. try to use single quotes in PHP. all HTML code uses double quotation marks.
3. double quotation marks can be used to simplify operations when variables are included.
4. wrap it in braces in complex cases
Another use of PHP quotation marks is that sometimes you need to use php to generate a text file. line feed n needs to use double quotation marks to make it easier. a single quotation mark will directly use n as a character output.
Usage summary: When variables, single quotation marks ('), and backslash (\) are not required in a string, try to quote the string with single quotation marks, because double quotation marks are not required to check the time on the conversion and parsing variables. Use single quotes as much as possible.