Further details PHP single and double quote differences

Source: Internet
Author: User

In PHP, single and double quotes are not interoperable, the specific difference is as follows:

The fields inside the double quotes are interpreted by the compiler and then exported as HTML code.

' Single quotes inside without explanation, direct output.

As you can see literally, single quotes are faster than double quotes.

For example:

$ABC = ' My name is tome ';
echo $ABC//Result: My name is Tom
The echo ' $abc '//result is: $ABC
echo "$ABC"//Result: My name is Tom


in the SQL statement
 
This is a recurring problem, when the SQL statement that inserts the database uses single quotes to define the string, and if you insert a string that contains single quotes into the database, the SQL statement will go wrong.
such as: $sql = "INSERT into UserInfo (Username,password) Values (' O ' kefee ', ' 123456 ')"
At this point, one of the ways to handle this is to add an escape character backslash to the SQL statement.
That is: ... Values (' o\ ' Kefee ',......
Of course, you can also use the function addslashes (), the function of which is to add an escape character,
namely: $s = addslashes ("O ' Kefee") ... Values (' ". $s." ',......
There is also a way to set the Magic-quotes option in PHP.ini, which opens the option so that if a single quote is in the information submitted by the form, it will automatically be added as an escape character. So you don't have to use other functions.
Add: This is going to start with double quotes and single quotes: The fields inside the double quotes are interpreted by the compiler and then exported as HTML code, but the single quote doesn't need to be interpreted and directly output.

$val = ' a ';

sql = "SELECT * from table where name like '% $val% '"; will automatically replace the inside $val, but if you want to organize the statement in single quotes, it is represented as:
sql = ' SELECT * FROM table where name like \ '% '. $val. '%\ '; A lot of trouble


But it's quicker to use single quotes instead of double quotes to include strings, because PHP searches for variables in double quotes, and single quotes don't.

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: "'"
1,2,3 partial strings are connected by "."


PHP Efficiency meter: Start with quotation marks (single quotes, double quotes efficiency difference)

Experiment one: efficiency under normal circumstances
Our experiment was to execute 10,000-byte files 5,000 times to get the speed of the parsing process.
First, this experiment specifically prescribes a unit, which is to better represent and eliminate the differences in the efficiency of the computer itself.
Time unit TT, representing about 7.2x10[sup]-8[/sup]s.
We obtained four sets of experimental data, which could be interpreted as representing a faster time for ' a ' than ' a ':
0.29 TT, about 0.0000000206s
0.46 TT, about 0.0000000328s
0.38 TT, about 0.0000000275s
0.41 TT, about 0.0000000292s
Average: 0.39 TT (' aaaa .... (139) ' efficiency is close to ' AAA ... (100) ")
Conclusion: The use of single quotes is faster in the same amount of data and under normal circumstances (without escaping).

Experiment two: The efficiency of single quotes escaping
Our experiment is 5,000 times respectively "single quotes, double quotes" to execute two copies of the same data, but one of the special uses of the "" to represent single quotes.
Time unit TT, representing about 7.2x10[sup]-8[/sup]s.
We obtained four sets of experimental data that could be interpreted as representing "faster than '" Time:
0.19 TT, about 0.0000000138s
0.15 TT, about 0.0000000110s
0.23 TT, about 0.0000000162s
0.24 TT, about 0.0000000173s
Average: 0.2 TT (""). (12) "efficiency close to ' \ ' ... (10) ")
Conclusion: If you need to represent single quotes (MySQL queries) in a string, avoid escaping as much as possible, so use double quotes.
Experiment Three: The efficiency of curly braces
Our experiment was performed 1000 times to execute a file containing a certain number of variable markers to get the speed of the parsing process.
Time unit TT, representing about 1.581x10[sup]-7[/sup]s~1.666x10[sup]-7[/sup]s. (TT change)
Gets three sets of data, indicating that "{$ABC}" is faster than "$ABC".
15.3 TT, about 0.0000024186s
14.5 TT, about 0.0000024093s
15.3 TT, about 0.0000024152s
This data is very alarming, the average result is TT.
This means that the parsing speed using curly braces is approximately 16 times times less than the curly braces.
Conclusion: variables are represented in double quotes, and curly braces are used whenever possible.

Experiment Four: The efficiency of continuous and discontinuous
Our experiment was performed 1000 times to execute a file containing a certain number of variable markers to get the speed of the parsing process.
Time unit TT, representing about 1.581x10[sup]-7[/sup]s~1.666x10[sup]-7[/sup]s. (TT change)
The first set of experimental data, representing "{$ABC} {$ABC} {$abc} ..." than $abc. $abc. $ABC Average resolution of each variable is fast
22.45 TT, about 0.0000035498s
21.03 TT, about 0.0000035037s
22.12 TT, about 0.0000034930s
Curly braces (continuous) are faster than single quotes (not consecutive), and the average result is TT.
This means that the parsing speed using curly braces is about 23 times times the use of discontinuous single quotes.
Conclusion: Multiple variables (or less data) are represented in single quotes, swapping double references whenever possible, and using curly braces.
The second group of experimental data indicates that "... $abc $abc$abc ..." $abc. $abc. $abc. ' ...' Average per variable resolution fast
7.15 TT, about 0.0000011311s
6.57 TT, about 0.0000010943s
6.83 TT, about 0.0000010777s
Double quotes (continuous) are faster than single quotes (not consecutive), averaging 7 TT. (This data changes)
This means that, for a long variable, the parse speed using double quotes (without curly braces) is approximately 8 times times the use of discontinuous single quotes.
Conclusion: Multiple variables (or less data) are represented in double quotes, which are faster than using single quotes instead of "Connection operators."
Conclusion: When multiple variables are represented (or the total amount of data is small), double quotes should be used in the range and curly braces are used.
(attached: The authors speculate that this may be related to the allocation of memory space, the continuous character may keep the memory adjusted)

Final conclusion:
First, when you represent simple data (you do not need to escape), try to use single quotes.
' Cal:are good at long jump? '
However, if you need to escape (that is, enclose single quotes) because you use single quotes, consider using double quotes.
' Cal:yes. But, you know, it\ ' s written. ' => ' Cal:yes. But, you know, it ' s written.
If you need to represent variables, you should use curly braces as much as possible.
"Cal: $to" => "cal: {$to}, shouldn ' t go shopping."
Try to make it continuous:
' Cal: '. $calsaid => ' cal: $calsaid ' => ' cal: {$calsaid} '

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.