我看到过很多人操作数组的时候, 对于数组中的非数字键名不使用引号
复制代码 代码如下:
$array[key] = $value;
复制代码 代码如下:
error_reporting = ~E_NOTIC
复制代码 代码如下:
$array = array();
$i = 0;
while(++$i $array['good'] = 2;
}
?>
复制代码 代码如下:
$array = array();
$i = 0;
while(++$i $array[good] = 2;
}
?>
复制代码 代码如下:
$ time php -f good.php
real 0m0.013s
user 0m0.005s
sys 0m0.007
复制代码 代码如下:
$ time php -f bad.php
PHP Notice: Use of undefined constant bad - assumed 'bad' in /home/huixinchen/tmp/bad.php
on line (此处省略999行NOTICE)
real 0m0.100s
user 0m0.020s
sys 0m0.029
复制代码 代码如下:
$ time php -f bad.php
real 0m0.037s
user 0m0.018s
sys 0m0.018
复制代码 代码如下:
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
复制代码 代码如下:
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
复制代码 代码如下:
define('key_name' , 'laruence');
....
//省略很多行代码
$array[key_name] = 2; //变成了 $array['laruence'] = 2;
//这样的错误, 你会很郁闷吧?
复制代码 代码如下:
$string = "variable value is {$array['key']}"
复制代码 代码如下:
....
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;//获取"常量"名字符串
zval_copy_ctor(&EX_T(opline->result.u.var).tmp_var);//分配空间,生成字符串
}
....