Necessity of parsing quotation marks for non-numeric key names in arrays _ PHP Tutorial

Source: Internet
Author: User
It is necessary to parse the quotation marks of non-numeric keys in the array. I have seen many people do not use quotation marks to copy the code for non-numeric key names in the array when operating the array: $ array [key] $ value; I can understand that some people may think that I have seen many people operate arrays without quotation marks for non-numeric key names in the array.

The code is as follows:


$ Array [key] = $ value;


I can understand that some people may think that such code is neat and can be executed normally.
Even more, if he is lucky to be well configured with php:

The code is as follows:


Error_reporting = ~ E_NOTIC


He may always be immersed in his "clean" style, and cannot see any NOTICE prompts. he will not realize how much performance he will lose when doing so ~
Let's take a look:
Good. php:

The code is as follows:


$ Array = array ();
$ I = 0;
While (++ $ I <1000 ){
$ Array ['good'] = 2;
}
?>


Bad. php:

The code is as follows:


$ Array = array ();
$ I = 0;
While (++ $ I <1000 ){
$ Array [good] = 2;
}
?>


Run time (average time multiple times ):
Enclosed in quotation marks:

The code is as follows:


$ Time php-f good. php
Real 0m0. 013 s
User 0m0. 005 s
Sys 0m0. 007


Without quotation marks:

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 (999 rows of NOTICE are omitted here)
Real 0m0. 100 s
User 0m0. 020 s
Sys 0m0. 029


How big is the difference?
Oh, maybe we should simulate the "lucky" people's situations and remove the expenses spent on recording NOTICE ~

The code is as follows:


$ Time php-f bad. php
Real 0m0. 037 s
User 0m0. 018 s
Sys 0m0. 018


We can see that, basically, the efficiency loss of using quotation marks or not using quotation marks is more than three times.
So where are these efficiency losses?
Let's take a look at the OPCODE sequence generated by the two files:
Good. php:

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:

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


As we can see (in fact, we also know according to the NOTICE prompt), PHP will regard the key name that is not enclosed by quotation marks as a constant for obtaining. when it cannot be found, it will throw a NOTICE, then generate a string based on the constant, and then use this string as the key name to continue ~
If you are smart, you will surely think that the following unexpected errors may occur:

The code is as follows:


Define ('key _ name', 'laruence ');
....
// Omit many lines of code
$ Array [key_name] = 2; // changed to $ array ['laruence '] = 2;
// You will be very depressed about this error?


Do you understand? Key names of non-numeric keys in the array must be enclosed in quotation marks ~
Oh, some people may say that when the string variable is replaced, writing quotation marks will lead to errors,
Well, standard writing:

The code is as follows:


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


I agree very much: "be lazy", but lazy should also be principled.
Finally, good code should not be disguised by disabling error_reporting.
Note: the related logic of constants cannot be found in FETCH_CONSTANT OPCODE:

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; // obtain the "constant" name string
Zval_copy_ctor (& EX_T (opline-> result. u. var). tmp_var); // allocate space and generate a string
}
....

The following code does not use quotation marks for non-numeric key names in the array: $ array [key] = $ value; I can understand that some people may think this...

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.