Skip to main content

php隐式转换大坑

· 3 min read

php是弱类型语言,其中一个坑是隐式转换

什么时候会触发隐式转换

这个我只知道比较的时候如果有相应的隐式转换。

例子

下面有个例子

php 代码

<?php
var_dump('1abc'== 1);
// 返回 true

经过_is_numeric_string_ex转换后,将1abc转换成了1

(gdb) p *lval
$4 = 1

堆栈如下

(gdb) bt
#0 _is_numeric_string_ex (str=0x7fffef602b58 "1abc", length=4, lval=0x7fffffff99a0, dval=0x7fffffff99a0, allow_errors=1, oflow_info=0x0) at /home/dinosaur/Downloads/php-7.2.2/Zend/zend_operators.c:3001
#1 0x0000000000938c52 in is_numeric_string_ex (str=0x7fffef602b58 "1abc", length=4, lval=0x7fffffff99a0, dval=0x7fffffff99a0, allow_errors=1, oflow_info=0x0)
at /home/dinosaur/Downloads/php-7.2.2/Zend/zend_operators.h:142
#2 0x0000000000938c94 in is_numeric_string (str=0x7fffef602b58 "1abc", length=4, lval=0x7fffffff99a0, dval=0x7fffffff99a0, allow_errors=1)
at /home/dinosaur/Downloads/php-7.2.2/Zend/zend_operators.h:146
#3 0x000000000094502b in compare_function (result=0x7fffffff9b78, op1=0x7fffffff9aa8, op2=0x7fffffff9ac8) at /home/dinosaur/Downloads/php-7.2.2/Zend/zend_operators.c:2069
#4 0x0000000000945a32 in is_equal_function (result=0x7fffffff9b78, op1=0x7fffffff9aa8, op2=0x7fffffff9ac8) at /home/dinosaur/Downloads/php-7.2.2/Zend/zend_operators.c:2159
#5 0x00000000009274e3 in zend_try_ct_eval_binary_op (result=0x7fffffff9b78, opcode=17, op1=0x7fffffff9aa8, op2=0x7fffffff9ac8) at /home/dinosaur/Downloads/php-7.2.2/Zend/zend_compile.c:6880
#6 0x0000000000927a0d in zend_compile_binary_op (result=0x7fffffff9b70, ast=0x7fffef686090) at /home/dinosaur/Downloads/php-7.2.2/Zend/zend_compile.c:6999
#7 0x000000000092b8d1 in zend_compile_expr (result=0x7fffffff9b70, ast=0x7fffef686090) at /home/dinosaur/Downloads/php-7.2.2/Zend/zend_compile.c:8235
#8 0x000000000091b84f in zend_compile_args (ast=0x7fffef6860a8, fbc=0x167f050) at /home/dinosaur/Downloads/php-7.2.2/Zend/zend_compile.c:3202
#9 0x000000000091baaf in zend_compile_call_common (result=0x7fffffff9d20, args_ast=0x7fffef6860a8, fbc=0x167f050) at /home/dinosaur/Downloads/php-7.2.2/Zend/zend_compile.c:3282
#10 0x000000000091e44b in zend_compile_call (result=0x7fffffff9d20, ast=0x7fffef6860d8, type=0) at /home/dinosaur/Downloads/php-7.2.2/Zend/zend_compile.c:4009
#11 0x000000000092bc3e in zend_compile_var (result=0x7fffffff9d20, ast=0x7fffef6860d8, type=0) at /home/dinosaur/Downloads/php-7.2.2/Zend/zend_compile.c:8339
#12 0x000000000092b841 in zend_compile_expr (result=0x7fffffff9d20, ast=0x7fffef6860d8) at /home/dinosaur/Downloads/php-7.2.2/Zend/zend_compile.c:8217
#13 0x000000000092b513 in zend_compile_stmt (ast=0x7fffef6860d8) at /home/dinosaur/Downloads/php-7.2.2/Zend/zend_compile.c:8186
#14 0x000000000092b0de in zend_compile_top_stmt (ast=0x7fffef6860d8) at /home/dinosaur/Downloads/php-7.2.2/Zend/zend_compile.c:8072
#15 0x000000000092b0c0 in zend_compile_top_stmt (ast=0x7fffef686018) at /home/dinosaur/Downloads/php-7.2.2/Zend/zend_compile.c:8067
#16 0x00000000008ec355 in zend_compile (type=2) at Zend/zend_language_scanner.l:601
#17 0x00000000008ec4e6 in compile_file (file_handle=0x7fffffffca10, type=8) at Zend/zend_language_scanner.l:635
#18 0x00000000007296f0 in phar_compile_file (file_handle=0x7fffffffca10, type=8) at /home/dinosaur/Downloads/php-7.2.2/ext/phar/phar.c:3320
#19 0x00007fffeeeca612 in opcache_compile_file (file_handle=0x7fffffffca10, type=8, key=0x7fffef16dd4c <accel_globals+556> "test.php:240416:240464", op_array_p=0x7fffffffa318)
at /home/dinosaur/Downloads/php-7.2.2/ext/opcache/ZendAccelerator.c:1600
#20 0x00007fffeeecb722 in persistent_compile_file (file_handle=0x7fffffffca10, type=8) at /home/dinosaur/Downloads/php-7.2.2/ext/opcache/ZendAccelerator.c:1941
#21 0x000000000094ccb4 in zend_execute_scripts (type=8, retval=0x0, file_count=3) at /home/dinosaur/Downloads/php-7.2.2/Zend/zend.c:1490
#22 0x00000000008b0b4a in php_execute_script (primary_file=0x7fffffffca10) at /home/dinosaur/Downloads/php-7.2.2/main/main.c:2590
#23 0x0000000000a3fd23 in do_cli (argc=2, argv=0x1441f40) at /home/dinosaur/Downloads/php-7.2.2/sapi/cli/php_cli.c:1011
#24 0x0000000000a40ee0 in main (argc=2, argv=0x1441f40) at /home/dinosaur/Downloads/php-7.2.2/sapi/cli/php_cli.c:1404


这是什么规则呢? 1 如果一个操作数是string ,一个是number ,会将string 转换成int ,如果转换发现不是数字就转换成0 然后他们就相等了

规则

操作数1操作数2规则
string,resource 或 numberstring,resource 或 number将字符串和资源转换成数字,按普通数学比较

相关阅读