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)?