· 作者: Laruence( )
· 本文地址: http://www.laruence.com/2009/04/24/695.html
· 转载请注明出处
我看到过很多人操作数组的时候, 对于数组中的非数字键名不使用引号,
1. $array[key] = $value;
我可以理解有些人可能会觉得这样的代码很”整洁”, 并且也能正常执行.
更甚至,如果他很”幸运的”php配置的好:
1. error_reporting = ~E_NOTICE
他也许永远都沉浸在自己的”整洁”风格中, 看不到任何的NOTICE提示, 也不会意识到, 他这么做, 能损失多少的性能~
来, 我们一起来看看:
1. good.php:
2. <?php
3. $array = array();
4. $i = 0;
5. while(++$i < 1000){
6. $array['good'] = 2;
7. }
8. ?>
9.
10. bad.php:
11. <?php
12. $array = array();
13. $i = 0;
14. while(++$i < 1000){
15. $array[good] = 2;
16. }
17. ?>
分别看运行时间(多次平均时间):
加引号的:
1. $ time php -f good.php
2.
3. real 0m0.013s
4. user 0m0.005s
5. sys 0m0.007s
不加引号的:
1. $ time php -f bad.php
2.
3. PHP Notice: Use ofundefined constant bad - assumed 'bad' in /home/huixinchen/tmp/bad.php
4. on line (此处省略999行NOTICE)
5. real 0m0.100s
6. user 0m0.020s
7. sys 0m0.029s
看看,差别有多大?
哦, 或许我们应该模拟一下那些”幸运的”人们的情况, 去掉花费在记录NOTICE的开销, 看看~
1. $ time php -f bad.php
2.
3. real 0m0.037s
4. user 0m0.018s
5. sys 0m0.018s
我们可以看出, 基本上, 使用引号,和不使用引号的效率损失在3倍以上
那么, 这些效率损失到哪里去了呢?
我们分别看下, 俩个文件生成的OPCODE序列:
good.php:
1. filename: /home/huixinchen/tmp/good.php
2. compiled vars: !0= $array, !1 = $i
3. line # op fetch ext return operands
4. -------------------------------------------------------------------------------
5. 2 0 INIT_ARRAY ~0
6. 1 ASSIGN !0, ~0
7. 3 2 ASSIGN !1, 0
8. 4 3 PRE_INC $3 !1
9. 4 IS_SMALLER ~4 $3, 1000
10. 5 JMPZ ~4, ->9
11. 5 6 ZEND_ASSIGN_DIM !0,'good'
12. 7 ZEND_OP_DATA 2,$6
13. 6 8 JMP ->3
14. 8 9 RETURN 1
15. 10*ZEND_HANDLE_EXCEPTION
bad.php:
1. filename: /home/huixinchen/tmp/bad.php
2. compiled vars: !0= $array, !1 = $i
3. line # op fetch ext return operands
4. -------------------------------------------------------------------------------
5. 2 0 INIT_ARRAY ~0
6. 1 ASSIGN !0, ~0
7. 3 2 ASSIGN !1, 0
8. 4 3 PRE_INC $3 !1
9. 4 IS_SMALLER ~4 $3, 1000
10. 5 JMPZ ~4, ->10
11. 5 6 FETCH_CONSTANT ~5 'bad'
12. 7 ZEND_ASSIGN_DIM !0,~5
13. 8 ZEND_OP_DATA 2, $7
14. 6 9 JMP ->3
15. 8 10 RETURN 1
16. 11*ZEND_HANDLE_EXCEPTION
我们可以看出(其实,根据NOTICE的提示也知道), PHP会把没有引号引起来的键名当作是常量去获取, 当找不到的时候, 抛出一个NOTICE, 然后再根据”常量明”生成一个字符串, 然后再讲这个字符串做为键名继续~
聪明的你一定会想到, 可能会出现如下不可预期的错误:
1. define('key_name' , 'laruence');
2. ....
3. //省略很多行代码
4. $array[key_name] = 2;//变成了 $array['laruence'] = 2;
5. //这样的错误, 你会很郁闷吧?
明白了么? 数组中的非数字键的键名一定要有引号啊~
哦, 还记得有人会说, 那在字符串变量替换的时候,写引号会导致错误,
恩, 标准写法:
1. $string = "variable value is {$array['key']}"
我很赞同:”be lazy”, 但是, lazy也是应该有原则的.
最后, 好的代码,不应该通过关闭error_reporting来伪装.
附注, FETCH_CONSTANT OPCODE中找不到常量的相关逻辑:
1. ....
2. if (!zend_get_constant(opline->op2.u.constant.value.str.val,
3. opline->op2.u.constant.value.str.len, &EX_T(opline->result.u.var).tmp_var TSRMLS_CC)) {
4. zend_error(E_NOTICE, "Use of undefined constant %s - assumed'%s'",
5. opline->op2.u.constant.value.str.val,
6. opline->op2.u.constant.value.str.val);
7. EX_T(opline->result.u.var).tmp_var = opline->op2.u.constant;//获取"常量"名字符串
8. zval_copy_ctor(&EX_T(opline->result.u.var).tmp_var);//分配空间,生成字符串
9. }
10. ....