The difference between single and double quotes in PHP _php tips

Source: Internet
Author: User

① escaped characters are different

Escape characters (\) can be used in both single and double quotes, but only single quotes and escape escape characters that are enclosed in single quotes are escaped. If you enclose a string in double quotes (""), PHP knows more about the escape sequence of special strings.

 <?php
$str 1 = ' \ ', \\,\r\n\t\v\$\ ';
echo $str 1, ' <br/> ';
 
$str 2 = "\", \\,a\r\n\tb\v\$\ ' ";
echo $str 2, ' <br/> ';
? >

② differ in the resolution of variables
Variables that appear in single quote strings are not replaced by variable values. That is, PHP does not parse the variable in single quotes, but instead prints the variable name as it is. The most important point of a double quote string is that the variable name is substituted by the variable value, which means that the variable contained in the double quotation marks can be parsed.

 <?php
$age =;
$str 1 = ' I am $age years old ';
$str 2 = "I am $age years old";
echo $str 1, ' <br/> '; I am $age years old 
echo $str 2, ' <br/> ';//I am years old;
? >

③ parsing speed is different

Single quotes do not need to consider the resolution of variables, faster than double quotes. single quotes are recommended. Sometimes double quotes are handy, like piecing together SQL statements

Back slash

 Use single quotes
echo ' this\n is\r the blog\t of\\zhoumanhe\\ '; 
The value above using single quotes is this\n is\r the blog\t of\zhoumanhe\
 
Echo '
;
echo "
";
 
Use double quotes
echo "this\n is\r the blog\t of\\zhoumanhe\\"; 
The value above with double quotation marks is the "is" blog of\zhoumanhe\ 

Using SQL

Suppose a constant is used in a query condition, for example:

SELECT * from abc_table where user_name= ' abc ';

The SQL statement can be written as:

SQLSTR = "SELECT * from abc_table where user _name= ' abc '";

Suppose a variable is used in a query condition, for example:

$user _name = $_request[' user_name ']; String variables

Or

$user =array ("name" => $_request[' user_name ', "age" =>$_request[' age '];//array variable

The SQL statement can be written as:

SQLSTR = "SELECT * from abc_table where user_name = '". $user _name. "'";
SQLSTR = "SELECT * from abc_table where user_name = '". $user [' name ']. ” ‘ “;

Compare:

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 broken down into the following 3 sections:

1: "SELECT * FROM table where user_name = '"//fixed SQL statement
2: $user//variable
3: "'"

Attachment: We have also seen Echo ' <br/> '; tags in HTML are valid in both single and double quotes.

Summarize the use of the PHP quotes principle

1. The value of the string is enclosed in quotation marks

In 2.PHP, try to use single quotes, all HTML code with double quotes

3. When you include a variable, you can use double quotes to simplify the operation

4. In complex cases, wrap them in curly braces.

Another use of PHP quotes is that sometimes you need to use PHP to generate text files, line feed n need double quotes to work, single quotes will directly n as the character output.

Usage Summary: When you do not need to add a variable or single quotation mark (') and a backslash (\) in a string, try to enclose the string in single quotes as much as possible, because you omit double quotes to check the time that is spent on escaping and parsing variables. can use single quotation marks as much as possible in single quotes.

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.