What's the difference between working with a string in PHP with single and double quotes? which is more efficient? Test today and see the code below.
The test code is as follows:
$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've mentioned in the previous article, and finally how this code is executed:
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 ~ ' this+is+a+ ',!2
6 ASSIGN! 3, ~
9 7 init_string
8 add_string, ' this+is+a+ '
9 Add_var,!2
Ten ASSIGN! 4,
ECHO!3
ECHO! 4
ASSIGN! 2, ' this '
CONCAT to 2, ' +is+a+string '
5, ASSIGN!
init_string ~
Add_var ~ ~,!2
add_string ~ ~, ' +is+a+string '
ASSIGN! 6, ~
ECHO!5
ECHO!6
RETURN 1
23* zend_handle_exception
Note that No. 0 through 3rd op line, you can see that the double quotes and the single quotes produce the same opcodes without using the variable substitution.
Again: 4th to 12th, it can be found that, in the case of variable substitution, the use of double quotes and single quotes generated by the opcodes is not the same, we have to analyze the double quotation cases of opcodes:
7 init_string Initializes a string variable that is stored in the-a temporary variable.
8 Add_string writes the first part of the string.
9 Add_var writes a string that is replaced by a variable.
第16-28 the same as the line.
From here we can find that in the case of double quotation marks and the use of single quotes, the same logic, the execution of the experience is really different (because, opcodes for PHP is the final code of execution). The sheer number of opcods generated alone is enough to justify the fact that single quotes are used quickly.
As for the compile phase, the difference between double quotation marks and single quotes is also very large, I will give a number to illustrate: in the scanning phase, there are 14 terms for double quotes, and for single quotes, only 6.
Oh, is not after this analysis, you will be more clearly understand how to use single and double quotation marks in the future?
By the way, for a pure string that does not require a variable substitution, it is known that double quotation marks are good in this case, because they represent strings in C/s + +.
In addition, for the standard, the HTML attribute value should be used double quotation marks to include, so don't get used to single quotes, everywhere abuse OH reprint
Http://bbs.tianya.cn/post-stocks-1721824-1.shtml
Http://bbs.tianya.cn/post-stocks-1721829-1.shtml
Http://bbs.tianya.cn/post-stocks-1721831-1.shtml
The difference between single quotation marks and double quotation marks in PHP and the efficiency of execution