PHP single and double quote string efficiency _php tips

Source: Internet
Author: User
The simple answer is clearly feeble. Let's do an experiment today to see what the difference between single and double quotes is, who is fast and who is slow.
The test code is as follows:
Copy Code code 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 = ' this ';
$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 our previous article to see how our code was executed in the end:
Copy Code code 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. 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
Ten ASSIGN! 4, ~5
One by one ECHO!3
ECHO! 4
ASSIGN! 2, ' this '
CONCAT ~8! 2, ' +is+a+string '
ASSIGN 5, ~8
Init_string ~10
Add_var ~10 ~10,!2
Add_string ~10 ~10, ' +is+a+string '
ASSIGN! 6, ~10
ECHO!5
ECHO!6
Return 1
23* zend_handle_exception

Note that the No. 0 to 3rd op line shows that double quotes are the same as the opcodes generated by single quotes without using variable substitution.
Again: 4th to 12th, you can find that, in the case of variable substitution, using double quotes and single quotes generated by the opcodes is not the same, we analyze double quotes in the case of opcodes:
7 init_string Initializes a string variable that is stored in the ~5 temporary variable.
8 Add_string writes the first part of the string.
9 Add_var The string to replace the variable with the write.
第16-28 line.

From here we can see that in the case of using double quotes and using single quotes, the same logic goes through differently (because opcodes is the final execution code for PHP). Just from the number of opcods generated, it is sufficient to prove that it is really quick to use single quotes.

As for the compile phase, the difference between double quotes and single quotes is great, and I'll give you a number to illustrate: in the scanning phase, there are 14 rules for double quotes, and only 6 for single quotes.

Oh, after this analysis, you will be more clearly understand how to use single and double quotes in the future?
By the way, for plain strings that do not require variable substitution, it is well known that double quotes represent strings in C + +, so it is better to use double quotes in this case.
In addition, the attribute values in HTML should be enclosed in double quotes, so don't get used to single quotes, 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.