PHP single quotes and double quotes string Efficiency

Source: Internet
Author: User

A simple answer is obviously pale and powerless. Let's make an experiment today to see what is the difference between single quotes and double quotes, who is fast and who is slow.
The test code is as follows:
Copy codeThe Code is as follows:
<? Php
$ Single_quotes = 'this is a string ';
$ Double_quotes = "This is a String ";
Echo $ single_quotes;
Echo $ double_quotes;
$ Var = 'string ';
$ Single_quotes_var = 'this is A'. $ var;
$ Double_quotes_var = "This is a $ var ";
Echo $ single_quotes_var;
Echo $ double_quotes_var;
$ Var = 'eas ';
$ Single_quotes_var_pre = $ var. 'is a string ';
$ Double_quotes_var_pre = "$ var is a String ";
Echo $ single_quotes_var_pre;
Echo $ double_quotes_var_pre;
?>

Next, let's take a look at the Opcodes generator we mentioned in previous articles, and finally how our code is executed:
Copy codeThe Code is as follows:
Branch analysis from position: 0
Return found
Filename:/home/xinchen/string. php
Function name: (null)
Number of ops: 24
Compiled vars :! 0 = $ single_quotes ,! 1 = $ double_quotes ,! 2 = $ var ,! 3 = $ single_quotes_var ,! 4 = $ double_quotes_var ,! 5 = $ single_quotes_var_pre ,! 6 = $ double_quotes_var_pre
Line # op fetch ext return operands
-------------------------------------------------------------------------------
2 0 ASSIGN! 0, 'this + is + a + string'
3 1 ASSIGN! 1, 'this + is + a + string'
4 2 ECHO! 0
5 3 ECHO! 1
7 4 ASSIGN! 2, 'string'
8 5 CONCAT ~ 3 'this + is + a + ',! 2
6 ASSIGN! 3 ,~ 3
9 7 INIT_STRING ~ 5
8 ADD_STRING ~ 5 ~ 5, 'this + is + a +'
9 ADD_VAR ~ 5 ~ 5 ,! 2
10 ASSIGN! 4 ,~ 5
11 11 ECHO! 3
12 12 ECHO! 4
14 13 ASSIGN! 2, 'eas'
16 14 CONCAT ~ 8! 2, '+ is + a + string'
15 ASSIGN! 5 ,~ 8
17 16 INIT_STRING ~ 10
17 ADD_VAR ~ 10 ~ 10 ,! 2
18 ADD_STRING ~ 10 ~ 10, '+ is + a + string'
19 ASSIGN! 6 ,~ 10
19 20 ECHO! 5
20 21 ECHO! 6
22 22 RETURN 1
23 * ZEND_HANDLE_EXCEPTION

Note that there are 0th to 3rd op lines. We can see that the double quotation marks and the Opcodes produced by single quotation marks are the same without variable replacement.
Let's take a look at the 4th-12th entries. We can find that, when using variable replacement, the Opcodes generated by using double quotation marks and single quotation marks are different. We will analyze the Opcodes in double quotation marks:
7 INIT_STRING initializes a string variable, which is stored in ~ 5.
8 ADD_STRING writes the first part of the string.
9 ADD_VAR writes the string replaced by the variable.
Line 16-28 is the same.

From this point, we can find that the execution of the same logic is indeed different when double quotation marks are used and when single quotation marks are used (because, opcodes is the final Execution Code for PHP ). From the number of generated Opcods alone, it is enough to prove that it is faster to use single quotes.

As for the compilation phase, double quotation marks and single quotation marks are also very different. I will give a number to illustrate: In the scanning phase, there are 14 lexical rules for double quotation marks, while for single quotation marks, there are only 6 entries.

Well, after such analysis, do you know more about how to use single quotes and double quotes?
By the way, for pure strings that do not require variable replacement, we all know that in C/C ++, double quotation marks indicate strings. In this case, it is better to use double quotation marks.
In addition, for W3C standards, attribute values in HTML should be included in double quotation marks, so do not get used to single quotation marks and abuse them everywhere.

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.