douxia3505 2019-03-27 12:19
浏览 60

为什么在算术运算中NULL被解释为零? [重复]

This question already has an answer here:

I never knew that PHP (at least ver. 5.6.25) considers NULL as 0 while performing simple arithmetic operations.

Recently, while hunting for the cause of a bug, I came across this. A simple test (indeed) confirms this:

$a = null;

echo $a + 1;    // 1

echo $a - 1;    // -1

echo $a * 1;    // 0

echo $a / 1;    // 0

echo 1 / $a;    // INF (**E_WARNING : type 2 -- Division by zero**)

From my "limited" knowledge of C, NULL is (void *)0. Now, my guess is that PHP "magic" type casting is somehow casting void * to int *?!!

Why did PHP do this? Isn't such "magic" casting (of null) something which should have been left for explicit conversions? What exactly is happening here (if my guess was wrong)?

</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 kali环境运行volatility分析android内存文件,缺profile
    • ¥15 写uniapp时遇到的问题
    • ¥15 vs 2008 安装遇到问题
    • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
    • ¥15 找一个网络防御专家,外包的
    • ¥100 能不能让两张不同的图片md5值一样,(有尝)
    • ¥15 informer代码训练自己的数据集,改参数怎么改
    • ¥15 请看一下,学校实验要求,我需要具体代码
    • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
    • ¥20 MATLAB绘制两隐函数曲面的交线