The necessity of parsing array non-numeric key name quotes _php Tutorial

Source: Internet
Author: User
Tags vars
I've seen a lot of people. When manipulating arrays, do not use quotation marks for non-numeric key names in arrays
Copy CodeThe code is as follows:
$array [key] = $value;

I can understand that some people may find this code very "neat" and can be executed properly.
Even if he is very "lucky" PHP configuration is good:
Copy CodeThe code is as follows:
error_reporting = ~e_notic

He may be immersed in his "neat" style forever, not seeing any notice hints, nor realizing how much performance he can lose in doing so.
Come on, let's take a look:
good.php:
Copy CodeThe code is as follows:
$array = Array ();
$i = 0;
while (+ + $i < 1000) {
$array [' good '] = 2;
}
?>

bad.php:
Copy CodeThe code is as follows:
$array = Array ();
$i = 0;
while (+ + $i < 1000) {
$array [Good] = 2;
}
?>

See the elapsed time (multiple average time) separately:
in quotation marks:
Copy CodeThe code is as follows:
$ Time Php-f good.php
Real 0m0.013s
User 0m0.005s
SYS 0m0.007

non-quoted:
Copy CodeThe 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 (omitted here 999 lines notice)
Real 0m0.100s
User 0m0.020s
SYS 0m0.029

Look, how big is the difference?
Well, maybe we should simulate the "lucky" people's situation, take away the expense of recording notice, and see ~
Copy CodeThe code is as follows:
$ Time Php-f bad.php
Real 0m0.037s
User 0m0.018s
SYS 0m0.018

We can see that basically, the use of quotes, and the loss of efficiency without using quotation marks is more than 3 times times
So where does the loss of efficiency go?
Let's take a look at the sequence of opcode generated by two files:
good.php:
Copy CodeThe 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 $!1
4 Is_smaller ~ $1000
5 Jmpz ~,->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 CodeThe 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 $!1
4 Is_smaller ~ $1000
5 Jmpz ~,->10
5 6 Fetch_constant "Bad"
7 Zend_assign_dim! 0,
8 Zend_op_data 2, $7
6 9 JMP->3
8 RETURN 1
11* Zend_handle_exceptio

We can see (in fact, according to notice hints also know), PHP will not enclose the key name as a constant to get, when not found, throw a notice, and then according to "Constant Ming" to generate a string, and then say the string as the key name continue ~
Smart you will think that there may be an unexpected error such as the following:
Copy CodeThe code is as follows:
Define (' Key_name ', ' laruence ');
....
Omit many lines of code
$array [Key_name] = 2; became the $array [' laruence '] = 2;
Would you be depressed by such a mistake?

Do you get it? The key name of the non-numeric key in the array must have quotation marks.
Oh, remember when someone would say that when a string variable is replaced, writing quotes can cause errors,
well, the standard notation:
Copy CodeThe code is as follows:
$string = "Variable value is {$array [' key ']}"

I agree: "Be lazy", but lazy should also have principles.
Finally, the good code should not be disguised by shutting down error_reporting.
Note, the associated logic for constants is not found in fetch_constant opcode:
Copy CodeThe 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, generate string
}
....

http://www.bkjia.com/PHPjc/328122.html www.bkjia.com true http://www.bkjia.com/PHPjc/328122.html techarticle I've seen a lot of people. When manipulating arrays, do not use quotation marks for non-numeric key names in the array to copy code as follows: $array [key] = $value; I can understand some people might 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.