In php, single quotes and double quotes define strings differently.

Source: Internet
Author: User
Single quotation marks and double quotation marks can parse variables for defining characters, and directly output variables. at the same time, single quotation marks and double quotation marks must be superior to double quotation marks in character processing. ① escape characters () can be used in different single quotation marks and double quotation marks, but can only be escaped in single... single quotation marks and double quotation marks can parse variables for defining characters, and directly output variables. at the same time, single quotation marks and double quotation marks must be superior to double quotation marks in character processing.

① Escape characters are different

You can use escape characters (\) in single quotation marks and double quotation marks, but you can only escape the single quotation marks and escape characters cited in single quotation marks. if you use double quotation marks ("") to enclose strings, PHP understands more escape sequences of special strings. the code is as follows:

 '; $str2 = "\",\\,a\r\n\tb\v\$\'"; echo $str2,'
';

② Different variables are parsed.

Variables in single quotes strings are not replaced by variable values. that is, PHP does not parse the variables in single quotes, but outputs the variable names as they are, the most important aspect of a double quotation mark string is that the variable name is replaced by the variable value, that is, the variable contained in double quotation marks can be parsed. the code is as follows:

 '; // I am $age years old  echo $str2,'
'; // I am 20 years old;

③ Different resolution speeds

Variable parsing is not required for single quotes, and the speed is faster than double quotes. we recommend that you use single quotes. sometimes double quotes are easier to use, such as assembling SQL statements.

The backslash code is as follows:

// Use single quotes echo 'This \ n is \ r the blog \ t of \ zhoumanhe \\'; // the value output using single quotes above is this \ n is \ r the blog \ t of \ zhoumanhe \ echo ''; echo "";

// Use double quotation marks

echo "this \n is \r the blog \t of \\ zhoumanhe \\";

// The value output using double quotation marks above is this is the blog of \ zhoumanhe \

The SQL code is as follows. assume that a constant is used in the query condition, 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']; // the SQL statement of the array variable can be written as: 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 :"'"

Note: You have also seen echo'
'; Tags in html are valid in single quotes and double quotation marks.

To sum up the usage principles of PHP quotes

1. the string value is enclosed in quotation marks.

2. try to use single quotes in PHP. all HTML code uses double quotation marks.

3. double quotation marks can be used to simplify operations when variables are included.

4. wrap it in braces in complex cases

PHP quotation marks are also useful. sometimes you need to use php to generate a text file. line feed n requires double quotation marks to make it easy to use. single quotation marks directly use n as a character output.

Usage summary: When variables, single quotation marks ('), and backslash (\) are not required in a string, try to quote the string with single quotation marks, because double quotation marks are not used to check the conversion and time of parsing variables, single quotation marks can be used as much as possible.

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.