Home > Backend Development > PHP Tutorial > The necessity of parsing quotation marks for non-numeric key names in arrays_PHP Tutorial

The necessity of parsing quotation marks for non-numeric key names in arrays_PHP Tutorial

WBOY
Release: 2016-07-21 14:59:37
Original
928 people have browsed it

I have seen many people do not use quotation marks for non-numeric key names in the array when operating arrays

Copy code The code is as follows :

$array[key] = $value;

I can understand that some people may think that such code is "neat", and it can also execute normally.
Even more, if he is "lucky" with good php configuration:
Copy the code The code is as follows:

error_reporting = ~E_NOTIC

He may always be immersed in his "neat" style, not seeing any NOTICE prompts, and not realizing how much performance he can lose by doing so. ~
Come, let’s take a look:
good.php:
Copy the code The code is as follows:

$array = array();
$i = 0;
while(++$i < 1000){
$array['good'] = 2;
}
?>

bad.php:
Copy code The code is as follows:

$array = array();
$i = 0;
while(++$i < 1000){
$array[good] = 2;
}
?>

Look at the running time separately (multiple average times):
In quotes:
Copy code The code is as follows:

$ time php -f good.php
real 0m0.013s
user 0m0.005s
sys 0m0.007

Without quotation marks:
Copy code The code is as follows:

$ time php -f bad.php
PHP Notice: Use of undefined constant bad - assumed 'bad' in /home/huixinchen/tmp/bad.php
on line (this Line 999 is omitted NOTICE)
real 0m0.100s
user 0m0.020s
sys 0m0.029

Look, how big the difference is?
Oh, maybe we should simulate the situation of those "lucky" people and remove the overhead spent on recording NOTICE, take a look~
Copy code Code As follows:

$ time php -f bad.php
real 0m0.037s
user 0m0.018s
sys 0m0.018

us It can be seen that basically, the efficiency loss between using quotation marks and not using quotation marks is more than 3 times
So, where do these efficiency losses go?
Let’s look at them separately. OPCODE sequence generated from each file:
good.php :
Copy the code The code is as follows:

filename: /home/huixinchen/tmp/good.php
compiled vars: !0 = $array, !1 = $i
line # op fetch ext return operands
------ -------------------------------------------------- -----------------------
2 0 INIT_ARRAY ~0
1 ASSIGN !0, ~0
3 2 ASSIGN !1, 0
4 3 PRE_INC $3 !1
4 IS_SMALLER ~4 $3, 1000
5 JMPZ ~4, ->9
5 6 ZEND_ASSIGN_DIM !0, 'good'
7 ZEND_OP_DATA 2 , $6
6 8 JMP ->3
8 9 RETURN 1
10* ZEND_HANDLE_EXCEPTIO

bad.php :
Copy code The code is as follows:

filename: /home/huixinchen/tmp/bad.php
compiled vars: !0 = $array, !1 = $i
line # op fetch ext return operands
-- -------------------------------------------------- ----------------------------
2 0 INIT_ARRAY ~0
1 ASSIGN !0, ~0
3 2 ASSIGN !1, 0
4 3 PRE_INC $3 !1
4 IS_SMALLER ~4 $3, 1000
5 JMPZ ~4, ->10
5 6 FETCH_CONSTANT ~5 'bad'
7 ZEND_ASSIGN_DIM !0, ~5
8 ZEND_OP_DATA 2, $7
6 9 JMP ->3
8 10 RETURN 1
11* ZEND_HANDLE_EXCEPTIO

We can watch (Actually, you know from the NOTICE prompt), PHP will treat the key name without quotation marks as a constant to obtain. When it cannot be found, it will throw a NOTICE, and then generate a new one based on the "constant statement" string, and then use this string as the key name to continue~
If you are smart, you will definitely think that the following unpredictable errors may occur:
Copy codeThe code is as follows:

define('key_name' , 'laruence');
....
//Omit many lines of code
$array[key_name ] = 2; //It becomes $array['laruence'] = 2;
//Such an error will make you very depressed, right?

Do you understand? In the array The key names of non-numeric keys must have quotation marks~
Oh, I still remember someone saying that when replacing string variables, writing quotation marks will cause an error,
Well, the standard writing method is:
Copy code The code is as follows:

$string = "variable value is {$array['key']} "

I agree very much: "be lazy", but lazy should also have principles.
Finally, good code should not be disguised by turning off error_reporting.
Note, the relevant logic of constants cannot be found in FETCH_CONSTANT OPCODE:
Copy the code The code is as follows:

. ...
if (!zend_get_constant(opline->op2.u.constant.value.str.val,
opline->op2.u.constant.value.str.len, &EX_T(opline- >result.u.var).tmp_var TSRMLS_CC)) {
zend_error(E_NOTICE, "Use of undefined constant %s - assumed '%s'",
opline->op2.u.constant.value .str.val,
opline->op2.u.constant.value.str.val);
EX_T(opline->result.u.var).tmp_var = opline->op2.u .constant; //Get the "constant" name string
zval_copy_ctor(&EX_T(opline->result.u.var).tmp_var);//Allocate space and generate string
}
.. ..

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/328122.htmlTechArticleI have seen many people not using quotation marks for non-numeric key names in the array when operating arrays. Copy code The code is as follows: $array[key] = $value; I can understand that some people may think this...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template