The difference between single quotes and double quotes in PHP is described in detail.

Source: Internet
Author: User

The article details the differences between single quotes and double quotes in PHP. For more information, see.

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.

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.