The difference between single quotes and double quotes in php is very important in programming languages, both single quotes and double quotes. PHP quotes are easier to use than ASP quotes. in ASP, to substitute data into a variable, you must use double quotation marks. In addition, you can only use single quotation marks when using the quotation marks, double quotation marks are not allowed. if the double php single quotation marks are used, the difference between double quotation marks
In programming languages, both single quotes and double quotes play an important role, as in PHP. PHP quotes are easier to use than ASP quotes. in ASP, to substitute data into a variable, you must use double quotation marks. In addition, you can only use single quotation marks when using the quotation marks, double quotation marks are not allowed. if double quotation marks are used, they are treated as the ending character of the previous quotation marks. But in PHP, there is no such restriction. Replace the value with a variable. single quotation marks and double quotation marks can be used, but must be used in pairs.
In PHP, if it is only used for text data that does not contain variables, there is no difference between single quotation marks and double quotation marks. However, to use variables, single quotes and double quotation marks are different. In PHP, variables can be directly substituted into double quotes without definition or other symbols, such as: $ B = "cat"; $ a = "Thisisa $ B "; // Display thisacat single quotes. if $ a = 'thisisa $ B ', it is displayed as: Thisisa $ B.
There is also a difference between single quotes and double quotes in terms of operation efficiency. Generally, the speed of single quotes is faster, and double quotes are slower. The reason is that the double quotation marks must first look for variables in the statement, you do not need to use single quotes. Therefore, use single quotes if the variables are not entered in the statement. This is a habit of writing programs, always thinking about improving program efficiency.
If you want to convert the definition operation in the statement, you must use double quotation marks. For example, when you define a single quotation mark, it is written as follows: $ a = 'He \' snameisTom. ', the program will put He \' snameisTom. if it is displayed as follows: $ a = "He \ 'snameisTom. ", the program will display: He 'snameistom.
Single quotation marks are a stumbling block to SQL statements. text data in SQL statements must be included in single quotation marks. Therefore, if single quotation marks appear in the data, the database considers the data to end, the subsequent data will be considered as other components of the SQL statement, and of course an error will be reported during database query. Therefore, the text data written into the SQL statement must use addslashes () the function is converted to single quotes, and stripslashes () is used for data reading.
From the single quotes and double quotes of PHP, we can see that PHP is more active than ASP, because it is flexible and considerate. we can see from the single quotes and double quotes.
Difference between single quotes and double quotes in PHP
In PHP, a string is usually defined in a pair of quotation marks, such:
'Iamastringinsinglequotes'
"Iamastringindoublequotes"
The PHP syntax analyzer uses pair quotation marks to determine a string. Therefore, all strings must use the same single or double quotation marks to define the start and end. For example, the following string definition is illegal:
"IamnotavalidstringsinceIhaveunmatchingquotemarks'
'Meneither! "
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 = "Iama 'singlequotestring 'insideadoublequotestring ";
$ S = 'iama "doublequotestring" insideasinglequotestring ';
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:
"Whydoesn't" this "work? "
In fact, PHP syntax analyzer is divided into three parts:
"Whydoesn' 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:
"Whydoesn'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 \ 'dbetterescapeyourapostrophes'
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 < <符号紧跟一个自定义字符串开头,最后一行以该自定义字符串结束,并且必须顶格。< p>
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. ='
For ($ I = 0; $ I <10; $ I ++ ){
$ Square = $ I * $ I;
$ Html. ='
}
$ Html. ='
';
Number |
Square |
';
'. $ I .' |
'. $ Square .' |
';
';
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 "foois $ foo"; // print the result: foois2
Echo 'foois $ Foo'; // print the result: foois $ foo
Echo "foois $ foo \ n"; // print the result: foois2 (line feed at the same time)
Echo 'foois $ foo \ n'; // print the result: foois $ 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 * fromuserswherelast_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 * fromuserswherelast_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 * fromuserswherelast_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 hiddeninputs form is used for data transmission.
Experience. The best way to set the value of hiddeninputs is to use the htmlspecialchars () function for encoding. The following statement can be
To transmit data that may contain double quotation marks normally:
">
1. define a string using quotation marks. To enclose quotation marks, the analyzer must ignore the original meaning of the regular quotation marks in a string. we add a backslash before the quotation marks to tell PHP: this quotation mark is a part of a string. the correct representation is as follows: a single quotation mark string can be used anywhere else, and the processing speed of a single quotation mark string in the script is faster, because the PHP syntax analyzer treats single quotation marks in a simple way, and the double quotation marks need to be parsed internally, it is more complicated, so the processing speed is slightly slow.
This... double quotes escape, single quotes do not escape
For example,/r/n is a line feed, but if you write a file in single quotes, it is not a line feed, but a character. if you write a file in double quotes, it is a line feed.
I. define strings using quotation marks
In PHP, a string is usually defined in a pair of quotation marks, such:
'Iamastringinsinglequotes'
"Iamastringindoublequotes"
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:
"IamnotavalidstringsinceIhaveunmatchingquotemarks'
'Meneither! "
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 = "Iama 'singlequotestring 'insideadoublequotestring ";
$ S = 'iama "doublequotestring" insideasinglequotestring ';
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:
"Whydoesn't" this "work? "
In fact, PHP syntax analyzer is divided into three parts:
"Whydoesn' 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:
"Whydoesn'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 \ 'dbetterescapeyourapostrophes'
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 < <符号紧跟一个自定义字符串开头,最后一行以该自定义字符串结束,并且必须顶格。< p>
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. ='
For ($ I = 0; $ I <10; $ I ++ ){
$ Square = $ I * $ I;
$ Html. ='
}
$ Html. ='
';
Number |
Square |
';
'. $ I .' |
'. $ Square .' |
';
';
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 "foois $ foo"; // print the result: foois2
Echo 'foois $ Foo'; // print the result: foois $ foo
Echo "foois $ foo \ n"; // print the result: foois2 (line feed at the same time)
Echo 'foois $ foo \ n'; // print the result: foois $ 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 * fromuserswherelast_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 * fromuserswherelast_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 * fromuserswherelast_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 hiddeninputs form is used for data transmission.
Experience. The best way to set the value of hiddeninputs is to use the htmlspecialchars () function for encoding. The following statement can be
To transmit data that may contain double quotation marks normally:
">
1. define a string using quotation marks. To enclose quotation marks, the analyzer must ignore the original meaning of the regular quotation marks in a string. we add a backslash before the quotation marks to tell PHP: this quotation mark is a part of a string. the correct representation is as follows: a single quotation mark string can be used anywhere else, and the processing speed of a single quotation mark string in the script is faster, because the PHP syntax analyzer treats single quotation marks in a simple way, and the double quotation marks need to be parsed internally, it is more complicated, so the processing speed is slightly slow.
This... double quotes escape, single quotes do not escape
For example,/r/n is a line feed, but if you write a file in single quotes, it is not a line feed, but a character. if you write a file in double quotes, it is a line feed.
Agree.
?