Wenku. baidu. comview3e6a238271fe910ef12df806 "what is the difference and usage between single quotes and double quotes ?" Now I will summarize the answer and write it into this short article. Fields in double quotation marks are interpreted by the compiler and then output as HTML code. The single quotes are output directly without explanation. Example: $ abcmynamei
Http://wenku.baidu.com/view/3e6a238271fe910ef12df806 "What is the difference and usage between single quotes and double quotes ?" Now I will summarize the answer and write it into this short article. Fields in double quotation marks are interpreted by the compiler and then output as HTML code. ''Is output directly without being interpreted in single quotes. Example: $ abc = 'My name I
Http://wenku.baidu.com/view/3e6a238271fe910ef12df806
"What is the difference and usage between single quotes and double quotes ?" Now I will summarize the answer and write it into this short article.
"" Fields in double quotation marks are interpreted by the compiler and then output as HTML code.
''Is output directly without being interpreted in single quotes.
For example:
$ Abc = 'My name is tome ';
Echo $ abc // The result is: my name is tom
Echo '$ abc' // The result is: $ abc
Echo "$ abc" // The result is: my name is tom.
Especially when using MYSQL statements, double quotation marks and single quotation marks are confusing for beginners. Here, we will give an example to describe them.
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 :"'"
Connect 1, 2, and 3 strings "."
I. Define strings using quotation marks
In PHP, a string is usually defined in a pair of quotation marks, such:
'I am a string in single quotes'
"I am a string in double quotes"
The PHP syntax analyzer uses pair quotation marks to determine a string. Therefore, all strings must use the same single or double
To define start and end. For example, the following string definition is illegal:
"I am not a valid string since I have unmatching quote marks'
'Me neither! "
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 is
The quotation marks are parsed by the analyzer. In this way, you can include any other character in the double quotation mark string, or even a single cited
. The following quotation marks are valid:
$ S = "I am a 'single quote string 'inside a double quote string ";
$ S = 'I am a "double quote string" inside a single quote string ';
When PHP encounters a quotation mark corresponding to the start of the string, it is considered to have reached the end of the string, so:
"Why doesn't" this "work? "
In fact, PHP syntax analyzer is divided into three parts:
"Why doesn' t" -- contains a single double quotation mark string
This -- redundant characters, which cannot be processed by the analyzer
"Work? "-- Common string
The above example attempts to include double quotation marks in the double quotation mark string, and the analyzer considers the string to be closed when the second double quotation mark is encountered.
. To enclose quotation marks, the analyzer must ignore the original meaning of the regular quotation marks in a string.
Add a backslash to tell PHP: this quotation mark is a part of the string. The correct expression is as follows:
"Why doesn't \" that \ "work? "
A common problem in English strings is the use of the marker, because it is a single quotation mark, which is very common in English strings.
(In english ). You must handle these characters with caution:
You \ 'd better escape your apostrophes'
We can see that the backslash has its special meaning in the string. When we need to include the backslash itself in the string, we need
Add a backslash before the symbol. For example:
$ File = "c: \ windows \ system. ini ";
Echo $ file; // The printed result is: c: windowssystem. ini.
$ File = "c :\\ windows \ system. ini ";
Echo $ file; // The printed result is: c: \ windows \ system. ini.
Another string definition method can eliminate the troubles of special characters and facilitate reference of long texts. This string definer
Method < <符号紧跟一个自定义字符串开头,最后一行以该自定义字符串结束,并且必须顶格。
2. String connection
A string can be connected using a string connector (.), for example:
$ First_name = 'Charlie ';
$ Last_name = 'brown ';
$ Full_name = $ first_name. ''. $ last_name;
A common purpose is to create a large HTML string code. The Value Pair (=) connector (.) can be abbreviated as a (. =) character.
Number, such:
$ Html ='
';$ Html. ='
Number |
Square |
';For ($ I = 0; $ I <10; $ I ++ ){$ Square = $ I * $ I;$ Html. ='
'. $ I .' |
'. $ Square .' |
';}$ Html. ='
';
3. Use variables in strings
This feature allows you to stick a large number of simple strings without using the Concatenation symbol. PHP allows us to directly include words in double quotation marks
String variables, 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.
The content in the string is generally considered a common character. For example:
$ 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 $ foo \ n"; // print the result: foo is 2 (line feed at the same time)
Echo 'foo is $ foo \ n'; // print the result: foo is $ foo \ n
As you can see, in single quotes, even the backslash also loses its extended meaning (except for inserting the backslash \ and the insert ticket
Quotation marks \'). Therefore, when you want to replace the variable in the string and include escape sequences such as \ n (linefeed), you should use double quotes
. The single quotation mark string can be used anywhere else. The processing speed of the single quotation mark string in the script is faster, because the PHP syntax analyzer
The processing of single quotation marks is simple, while the processing of double quotation marks is more complicated because the strings need to be resolved internally, so the processing speed is high.
Slow.
When you reference a complex variable combination in a string, some problems may occur. The following code works properly:
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];
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:
$ Var = 3;
Echo "value = {$ var}"; // print the result "value = 3"
Echo "value =\{ $ var}"; // print the result "value = {3 }"
Iii. slashes and SQL statements
Generating HTML code or SQL query statements is a common and interesting issue when writing PHP programs. Why,
Because this involves generating another type of code, you must carefully consider and follow the syntax and rules required by this Code.
.
Let's take a look at this example. If you want to query a database named "O" Keefe ", it is usually in the form of SQL statements.
Yes:
Select * from users where last_name = 'o \ 'keefe'
Note that the SQL statement must be escaped by a backslash. PHP provides some special functions to handle this problem.
The function AddSlashes ($ str) is used to automatically insert a backslash escape character to the quotation mark character in the string:
$ Last_name = "O 'Keefe ";
$ SQL = "select * from users where last_name = '". addslashes ($ last_name )."'";
In this example, you also need to enclose single quotes (SQL syntax Requirements) outside the last_name string, because the double
Quotation marks, so you do not need to use escape for this pair of single quotes. The following statement uses the equivalent form of a single quotation mark string:
$ SQL = 'select * from users where last_name = \ ''. addslashes ($ last_name ).'\'';
At any time you want to write strings in the database, you must ensure that the quotation marks Correctly Use escape characters. This is a lot of PHP
Common mistakes made by beginners.
Iv. Double quotation marks and HTML
Unlike SQL statements, double quotation marks ("double quotation marks") are often used to represent strings in standard HTML languages (many browsers currently have strong fault tolerance capabilities
Yes. You can use single quotes in HTML to represent strings without quotation marks. For example:
$ Html = ''. $ link .'';
$ Html = "$ link ";
The HTML language does not support backslash escape, which is required when the hidden inputs of the form is used for data transmission.
Experience. The best way to set the value of hidden inputs is to use the htmlspecialchars () function for encoding. The following statement can be
To transmit data that may contain double quotation marks normally:
Some people often ask me, I heard that it is faster to process strings in PHP with single quotes. If there is a variable to replace, is it faster to connect with single quotes or double quotes? A simple answer is obviously pale and powerless. Let's make an experiment today to see what is the difference between single quotes and double quotes, who is fast and who is slow.
The test code is as follows:
$ Single_quotes = 'this is a string ';
$ Double_quotes = "This is a String ";
Echo $ single_quotes;
Echo $ double_quotes;
$ Var = 'string ';
$ Single_quotes_var = 'this is A'. $ var;
$ Double_quotes_var = "This is a $ var ";
Echo $ single_quotes_var;
Echo $ double_quotes_var;
$ Var = 'eas ';
$ Single_quotes_var_pre = $ var. 'is a string ';
$ Double_quotes_var_pre = "$ var is a String ";
Echo $ single_quotes_var_pre;
Echo $ double_quotes_var_pre;
?>
Next, let's take a look at the Opcodes generator we mentioned in previous articles, and finally how our code is executed:
Branch analysis from position: 0
Return found
Filename:/home/xinchen/string. php
Function name: (null)
Number of ops: 24
Compiled vars :! 0 = $ single_quotes ,! 1 = $ double_quotes ,! 2 = $ var ,! 3 = $ single_quotes_var ,! 4 = $ double_quotes_var ,! 5 = $ single_quotes_var_pre ,! 6 = $ double_quotes_var_pre
Line # op fetch ext return operands
-------------------------------------------------------------------------------
2 0 ASSIGN! 0, 'this + is + a + string'
3 1 ASSIGN! 1, 'this + is + a + string'
4 2 ECHO! 0
5 3 ECHO! 1
7 4 ASSIGN! 2, 'string'
8 5 CONCAT ~ 3 'this + is + a + ',! 2
6 ASSIGN! 3 ,~ 3
9 7 INIT_STRING ~ 5
8 ADD_STRING ~ 5 ~ 5, 'this + is + a +'
9 ADD_VAR ~ 5 ~ 5 ,! 2
10 ASSIGN! 4 ,~ 5
11 11 ECHO! 3
12 12 ECHO! 4
14 13 ASSIGN! 2, 'eas'
16 14 CONCAT ~ 8! 2, '+ is + a + string'
15 ASSIGN! 5 ,~ 8
17 16 INIT_STRING ~ 10
17 ADD_VAR ~ 10 ~ 10 ,! 2
18 ADD_STRING ~ 10 ~ 10, '+ is + a + string'
19 ASSIGN! 6 ,~ 10
19 20 ECHO! 5
20 21 ECHO! 6
22 22 RETURN 1
23 * ZEND_HANDLE_EXCEPTION
Note that there are 0th to 3rd op lines. We can see that the double quotation marks and the Opcodes produced by single quotation marks are the same without variable replacement.
Let's take a look at the 4th-12th entries. We can find that, when using variable replacement, the Opcodes generated by using double quotation marks and single quotation marks are different. We will analyze the Opcodes in double quotation marks:
7 INIT_STRING initializes a string variable, which is stored in ~ 5.
8 ADD_STRING writes the first part of the string.
9 ADD_VAR writes the string replaced by the variable.
Line 16-28 is the same.
From this point, we can find that the execution of the same logic is indeed different when double quotation marks are used and when single quotation marks are used (because, opcodes is the final Execution Code for PHP ). From the number of generated Opcods alone, it is enough to prove that it is faster to use single quotes.
As for the compilation phase, double quotation marks and single quotation marks are also very different. I will give a number to illustrate: In the scanning phase, there are 14 lexical rules for double quotation marks, while for single quotation marks, there are only 6 entries.
Well, after such analysis, do you know more about how to use single quotes and double quotes?
By the way, for pure strings that do not require variable replacement, we all know that in C/C ++, double quotation marks indicate strings. In this case, it is better to use double quotation marks.
In addition, for W3C standards, attribute values in HTML should be included in double quotation marks, so do not get used to single quotation marks and abuse them everywhere.
Seeing this, I can think that light is a question of quotation marks. I have such a college question. I should study hard to avoid headaches in the future.
I hope you will pay attention to it.