Difference between Single and Double quotation marks in PHP

Source: Internet
Author: User

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:
PHPCode Copy codeThe Code is as follows: $ 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.Copy codeThe Code is as follows: $ 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 codeCopy codeThe Code is as follows: $ 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
$ 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, 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 include escape sequences such as \ n (linefeed. 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 a complex variable combination is referenced in a string, some problems may occur. The following code works properly:
PHP Code copy Code the code is as follows: Echo" value = $ foo ";
echo "value = $ A [$ I]";
echo "value = $ foo ";
echo "value = $ A [$ I]";

but the following code cannot get the expected result:
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 $ A
, new problems have emerged. When you want to reference the curly braces in a string, remember to use the Escape Character:
PHP Code copy Code the code is as follows: $ 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 inserted into the 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 'kebabay', '123')"
at this time, one of the processing methods is to 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 method is to set PHP. the magic-quotes option in ini. If this option is enabled, an escape character is automatically added to the information submitted through the form if there are single quotes. Therefore, you do not need to use other functions.
supplement: this should start with double quotation marks and single quotation marks: the fields in double quotation marks are interpreted by the compiler and then output as HTML code, but the fields in single quotation marks do not need to be explained, direct output.
example: copy Code the code is as follows: $ 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:Copy codeThe Code 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.

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 starts with a <symbol followed by a custom string, and the last line ends with the custom string, and must be a top-level.

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 = '<Table> ';
$ Html. = '<tr> <TD> Number </TD> <TD> Square </TD> </tr> ';
For ($ I = 0; $ I <10; $ I ++ ){
$ Square = $ I * $ I;
$ Html. = '<tr> <TD>'. $ I. '</TD> <TD>'. $ square. '</TD> </tr> ';
}
$ Html. = '</table> ';

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

The HTML code or SQL query statement is written in PHP.ProgramIt is often encountered and interesting. 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 = '<a href = "'. $ URL. '">'. $ link. '</a> ';
$ Html = "<a href = \" $ URL \ "> $ link </a> ";
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:
<Input type = hidden name = VaR value = "<? PHP echo htmlspecialchars ($ var)?> ">

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.