Home  >  Article  >  Backend Development  >  Single quotes and double quotes in PHP String efficiency_PHP tutorial

Single quotes and double quotes in PHP String efficiency_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:46:43850browse

A simple answer is obviously feeble. Today we will do an experiment to see what is the difference between single quotes and double quotes, who is faster and who is slower.
The test code is as follows:

Copy code The 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 mentioned in the previous article and see how our code is finally 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, 'This'
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

Pay attention to the 0th to 3rd op lines. It can be seen that without using variable substitution, Opcodes generated by double quotes and single quotes are the same.
Looking again: Items 4 to 12, you can find that when using variable substitution, the Opcodes generated by using double quotes and single quotes are different. Let’s analyze the Opcodes in the case of double quotes:
7 INIT_STRING initializes a string variable and stores it in ~5 temporary variables.
8 ADD_STRING writes the first part of the string.
9 ADD_VAR writes the string replaced by the variable.
The same applies to lines 16-28.

From here we can find that the execution experienced by the same logic is indeed different when using double quotes and when using single quotes (because, Opcodes are final for PHP execute code). The number of generated Opcods alone is enough to prove that using single quotes is faster.

As for the compilation stage, the difference between double quotes and single quotes is also very big. I will give you a number to illustrate: In the scanning stage, there are 14 lexical rules for double quotes, while for single quotes, there are only There are only 6 items.

Haha, after this analysis, will you have a clearer understanding of how to use single quotes and double quotes in the future?
By the way, for pure strings that do not require variable substitution, everyone knows that in C/C++, double quotes represent strings, so in this case, it is better to use double quotes.
In addition, according to W3C standards, attribute values ​​in HTML should be enclosed in double quotes, so don’t get used to single quotes and abuse them everywhere.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320096.htmlTechArticleThe simple answer is obviously feeble. Today we will do an experiment to see what is the difference between single quotes and double quotes, who is faster and who is slower. The test code is as follows: Copy code Code...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn