The necessity of parsing array non-numeric key quotes _php Tips

Source: Internet
Author: User
Tags vars
I've seen a lot of people. When manipulating arrays, do not use quotes for non-numeric key names in an array
Copy Code code as follows:

$array [key] = $value;

I can understand that some people might think that code is "neat" and can be executed properly.
Even more, if he's very "lucky" the PHP configuration is good:
Copy Code code as follows:

error_reporting = ~e_notic

He may always be immersed in his own "neat" style, can not see any notice hint, do not realize that he did so, how much loss of performance
Come, let's look at it together:
good.php:
Copy Code code as follows:

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

bad.php:
Copy Code code as follows:

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

See Run time (multiple average time) separately:
enclosed in quotes:
Copy Code code as follows:

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

not enclosed in quotes:
Copy Code code 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 lines omitted here notice)
Real 0m0.100s
User 0m0.020s
SYS 0m0.029

look, how big is the difference?
Well, maybe we should simulate those "lucky" people, get rid of the expense of recording notice, and see
Copy Code code as follows:

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

We can see that basically, using quotes, and not using quotes, the efficiency loss is more than 3 times times
So where are the loss of efficiency?
Let's look at the opcode sequence generated by the two files:
good.php:
Copy Code code as follows:

FileName:/home/huixinchen/tmp/good.php
Compiled VARs:!0 = $array,! 1 = $i
Line # OP fetch ext. operands
-------------------------------------------------------------------------------
2 0 Init_array ~0
1 ASSIGN! 0, ~0
3 2 ASSIGN! 1, 0
4 3 Pre_inc $!1
4 Is_smaller ~4 $1000
5 Jmpz ~4,->9
5 6 Zend_assign_dim! 0, ' good '
7 Zend_op_data 2, $
6 8 JMP->3
8 9 Return 1
10* Zend_handle_exceptio

bad.php:
Copy code code as follows:

FileName:/home/huixinchen/tmp/bad.php
Compiled vars:!0 = $array,! 1 = $i
Line # OP fetch ext. operands
------------------------------------------------------------------------- ------
   2 0 init_array ~0
         1 ASSIGN! 0, ~0
   3 2 ASSIGN! 1, 0
   4 3 pre_inc $! 1
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NB Sp;   4 Is_smaller ~4 $, 1000
         5 JMPZ,- >10
   5 6 fetch_constant ~5 ' bad '
         7 Zend_assign_dim! 0, ~5
         8 zend_op_data 2, $
    6 9 JMP->3
   8 return 1
        11* Zend_handle_exceptio

We can see (in fact, according to notice's hints, PHP will take a key name that is not enclosed in quotes as a constant, and when it is not found, throw a notice and then generate a string based on the "constant", and then the string is continued as the key name.
Smart you will think that there may be an unexpected error as follows:
Copy Code code as follows:

Define (' Key_name ', ' laruence ');
....
Omit a lot of line code
$array [Key_name] = 2; became $array [' laruence '] = 2;
You're going to be upset about this mistake?

You get it? The key name of the non-numeric key in the array must have quotes AH ~
Oh, remember someone would say that when a string variable is replaced, writing quotes can cause an error,
Well, the standard wording:
Copy Code code as follows:

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

I very much agree: "Be lazy", but, lazy also should have the principle.
Finally, good code should not be disguised by closing the error_reporting.
Note, the relevant logic for constants is not found in fetch_constant opcode:
Copy Code code 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 (Opline->result.u.var). Tmp_var);//Allocate space, generate string
}
....

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.