PHP single and double quote differences

Source: Internet
Author: User

About the difference between single and double quotation marks and efficiency issues. A lot of friends understand is not very clear, always thought that single quotation marks and double quotes in PHP are interoperable, until one day, found that single and double quotation marks when the error occurred to study. So let's talk about their differences today and hope that we don't get confused about it.
The fields inside the double quotation mark are interpreted by the compiler and then output as HTML code.
"The single quotation mark does not explain, the direct output."
It can be seen literally that single quotes are faster than double quotes.
For example:
$ABC = ' My name is tome ';
echo $ABC//Result: My name is Tom
Echo ' $ABC '//Result: $ABC
echo "$ABC"//Result: My name is Tom
Especially when using MySQL statements, the use of double and single quotes is confusing for novices, here, for example, to illustrate.
Suppose a constant is used in a query condition, for example:
SELECT * from abc_table where user_name= ' abc ';
SQL statements 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
SQL statements 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 decomposed into the following 3 parts:
1: "SELECT * FROM table where user_name = '"//fixed SQL statement
2: $user//variable
3: "'"
The "." Connection between the half-and-part strings
[P=30, 2, left]
[/P]

====

First think about all the words in PHP (actually called symbols) there are several categories.

1.php,mysql two-side keywords and functions. For example Echo,print,mysql_connect and so on. These are certainly not quoted.

2. Constants. Novice may not use much, the benefit of the constant is global, penetrating function. It's faster, but the novice can temporarily ignore the constant.

3. Variables. The "$" number is the variable. You can set a value for a variable, such as a string of characters, a number, a logical (TRUE/False) value, and so on. You can also represent a set of values (arrays, objects, and so on)

4. Value. You typically set values for constants and variables. The assignment statement $a=\ ' Abc\ ', the right \ ' abc\ ' is the value.

5. Parameters of the function (in parentheses). Can be a constant, a variable, a value of three.

The relationship between a variable (constant) and a value is as varied as the following conditions.

"Color" and "red",

"Length" with 100,

"Date" and October 25, 2007 "

Two. What is the case with PHP quotes

In fact, only the 4th "value" need to use quotation marks, the function is only the value of the quotation marks. and only strings (date values can be treated as strings) need to use quotation marks. The number (not available), is true or false (cannot be used) exception.

Example

Three. The difference between a single quote and a double quote

In general, both are generic. But the double quotes internal variable resolves, and the single quotation mark is not resolved.

Example

So if there is only a pure string inside, use a single quotation mark (fast), the interior has something else (such as a variable), with a few more points.

Four. What to do if PHP quotes appear inside the string-about escaping.

For example, we want to output: I'm a genius.

It must be escaped at this time. Escape is to turn the signed symbol into meaningless characters.

This is normal, because the number turns any character behind it into meaningless symbols. In this case, the PHP parser simply does not look at the quotation marks behind the number.

Also, you can escape special symbols such as semicolons, $ symbols, and so on.

Five. Connection of strings.

This is a troublesome question. In general, variable values are directly enclosed in double quotes. Additional strings are superimposed with "." Character.

In a complex situation where curly braces are included, PHP knows that this is a complete thing, and the quotes inside it do not affect the outside quotation marks.

The mix with HTML is also very simple, it is best to form all the HTML in double quotation marks, PHP as far as possible in the habit of single quotation marks. This makes it easy to copy large pieces of HTML code, as long as the tail and the single quotation mark is the correct string. The HTML code for hundreds of lines doesn't have to worry about PHP quotes.

Summarize the guidelines for using PHP quotes

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

Use single quotes as much as possible in 2.PHP, with double quotes in all HTML code

3. When you include variables, use double quotation marks to simplify the operation

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

PHP quotes also have a use, sometimes need to generate text files in PHP, newline character \ n need to use double quotation marks in order to work, single quotation marks will be directly to the character output.

PHP single and double quote differences

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.