The difference between single quotes and double quotes in PHP is described in detail. The article details the differences between single quotes and double quotes in PHP. For more information, see. 1. define a string in PHP. The definition of a string can be described in detail in the document about the differences between single quotes and double quotes in PHP. for details, refer.
1. define a string
In PHP, you can use single quotes or double quotes to define strings. However, the same single or double quotation marks must be used to define strings. for example, 'hello' and 'hello' are invalid strings.
When defining a string, only one type of quotation mark is considered as a definition character, that is, single quotation marks or double quotation marks. Therefore, if a string starts with double quotation marks, only the double quotation marks are parsed by the analyzer. In this way, you can include any other character in the double quotation marks, or even single quotation marks. The following quotation marks are valid:
Php code
$ S = "I am a 'single quote string 'inside a double quote string ";
$ S = 'I am a "double quote string" inside a single quote string ';
$ S = "I am a 'single quote string 'inside a double quote string ";
$ S = 'I am a "double quote string" inside a single quote string ';
And the string "Why doesn' t" this "work? "Is divided into three sections. If you want to express double quotation marks in this string, you can use the escape character "(backslash) to convert it to" Why doesn't "this" work? .
2. single and double quotation marks in string variables
PHP allows us to directly include string variables in double quotes. we can find that the processing results of the following two strings are the same.
$ Full_name = $ first_name. ''. $ last_name;
$ Full_name = "$ first_name $ last_name ";
The processing of single and double quotation marks is different in PHP. The content in the double quotation mark string can be interpreted and replaced, while the content in the single quotation mark string is always considered as a common character. For example:
Php code
$ Foo = 2;
Echo "foo is $ foo"; // print the result: foo is 2
Echo 'foo is $ Foo'; // print the result: foo is $ foo
Echo "foo is $ foon"; // print the result: foo is 2 (line feed at the same time)
Echo 'foo is $ foon '; // print the result: foo is $ foon
$ Foo = 2;
Echo "foo is $ foo"; // print the result: foo is 2
Echo 'foo is $ Foo'; // print the result: foo is $ foo
Echo "foo is $ foon"; // print the result: foo is 2 (line feed at the same time)
Echo 'foo is $ foon '; // print the result: foo is $ foon
As you can see, even the backslash in a single quotation mark loses its extended meaning (except for inserting the backslash \ and inserting the single quotation mark '). Therefore, you should use double quotation marks when you want to replace variables in strings and escape sequences containing n (linefeeds. Single quotation mark strings can be used anywhere else. the processing speed of single quotation mark strings in the script is faster, because the PHP syntax analyzer treats single quotation mark strings in a simple way, the processing of double quotation marks is more complicated because the strings also need to be parsed, so the processing speed is slightly slower.
When you reference a complex variable combination in a string, some problems may occur. the following code works properly:
Php code
Echo "value = $ foo ";
Echo "value = $ a [$ I]";
Echo "value = $ foo ";
Echo "value = $ a [$ I]";
However, the following code cannot get the expected results:
Echo "value = $ a [$ I] [$ j]"; // we want to print an element of the two-dimensional array $.
To avoid potential problems in the use of these strings, we usually separate complex variables from the strings, like this: echo 'value = '. $ a [$ I] [$ j]; // point (.) used for string connection (.)
Another way is to enclose complex variables in curly brackets so that the syntax analyzer can correctly identify them:
Echo "value = {$ a [$ I] [$ j]}" // print an element of a two-dimensional array $
In this way, new problems have emerged. When we want to reference the curly braces in a string, remember to use the escape character:
Php code
$ Var = 3;
Echo "value = {$ var}"; // print the result "value = 3"
Echo "value = {$ var}"; // print the result "value = {3 }"
$ Var = 3;
Echo "value = {$ var}"; // print the result "value = 3"
Echo "value = {$ var}"; // print the result "value = {3 }"
3. in SQL statements
This is a common problem. the SQL statement for inserting a database uses single quotes to define strings. if you want to insert a string containing single quotes into the database, this SQL statement will fail.
For example: $ SQL = "insert into userinfo (username, password) Values ('o 'keping', '123 ')"
In this case, add the escape character backslash to the SQL statement,
That is :...... Values ('o 'keping ',......
Of course, you can also use the addslashes () function. the Function adds an escape character,
That is, $ s = addslashes ("O 'keyun ")...... Values ('". $ s ."',......
Another way is to set the magic-quotes option in php. ini. if this option is enabled, if the single quotation marks are included in the information submitted through the form, an escape character such as is automatically added. Therefore, you do not need to use other functions.
Supplement: this should start with the function of double quotation marks and single quotation marks: the fields in double quotation marks will be interpreted by the compiler and then output as HTML code, but the fields in single quotation marks do not need to be interpreted and output directly.
For example:
$ Abc = 'I love u ';
Echo $ abc // The result is: I love u
Echo '$ ABC' // The result is: $ abc
Echo "$ abc" // The result is: I love u
Therefore, when assigning values to SQL statements in the database, you must use SQL = "select a, B, c from..." in double quotation marks. However, single quotation marks in SQL statements are used to quote field names.
Example: select * from table where user = 'abc ';
The SQL statement here can be directly written as SQL = "select * from table where user = 'abc '"
However, if it is as follows:
$ User = 'abc ';
SQL1 = "select * from table where user = '". $ user. "'"; comparison
SQL2 = "select * from table where user = 'abc '"
I add spaces between single quotes and double quotes. I hope you can see it clearly.
That is, replacing 'ABC' with '". $ user."' is in a single quotation mark. It only splits the entire SQL string. SQL1 can be divided into the following three parts:
1: "select * from table where user = '"
2: $ user
3 :"'"
Use. to connect strings.
Bytes. 1. define a string in PHP. The definition of a string can be...