0

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

yivi
  • 42,438
  • 18
  • 116
  • 138
Fr0zenFyr
  • 1,899
  • 2
  • 28
  • 48
  • https://stackoverflow.com/questions/137487/null-vs-false-vs-0-in-php/5700831 – Script47 Mar 27 '19 at 12:22
  • 7
    PHP is a loosely typed language, and does such automatic conversions on purpose. _You_ have to be explicit if that kind of behavior doesn’t work for you ;-) – 04FS Mar 27 '19 at 12:23
  • @script47 the link talk only about (strict) comparison, which I'm already aware of. – Fr0zenFyr Mar 27 '19 at 12:26
  • 1
    The point being that if you were expecting a numeric value to do math on then how did the null get through? That would only happen if you were doing loose comparison in your validation or you had no validation in place. – Script47 Mar 27 '19 at 12:28
  • @04FS: exactly my question. The casting should be explicit for `null` arithmetic. `undefined + int` can't be `int`. Why the negative vote? Is the question invalid? – Fr0zenFyr Mar 27 '19 at 12:30
  • The null comes from a nullable db field. Yes, i didn't check for null before arithmetic operation. Simply, tested for `int + db value > 0`. Expected the test to fail if db value was`null` – Fr0zenFyr Mar 27 '19 at 12:34
  • @04FS I'm completely aware that "PHP is a loosely typed language". My question is asking more about what's happening in the background (if my guess was incorrect)? – Fr0zenFyr Mar 27 '19 at 12:39
  • Now why is the question marked as a duplicate? Can anyone suggesting it as a duplicate may please also link to question(s) that talks about the same thing and not about `comparison` and `strict comparison`? This question is definitely not about `null == false` and `null !== false`. Here, I'm talking about `null` vs `int` but not about `null` vs `bool` – Fr0zenFyr Mar 27 '19 at 12:42
  • TL;DR PHP is weakly typed. NULL is considered equivalent to 0 and will be converted to `(int)0` for arithmetic. The dupes are not about *comparing* but the broader issue of [type juggling](https://www.php.net/manual/en/language.types.type-juggling.php) – Machavity Mar 27 '19 at 12:43
  • _“My question is asking more about what's happening in the background”_ - and what answer besides “PHP has decided that in this context, converting NULL to 0 makes the most sense” do you expect then? PHP is following its rules for automatic type casting here, that’s what’s “happening in the background”. Not really sure what else would merit discussing in that context. – 04FS Mar 27 '19 at 12:48
  • @Machavity: then why couldn't you link to a question that discusses about **type juggling** while marking this post as duplicate? I'd still be interested in reading a discussion about type juggling, if that is what answers this question. It is weird that SO allows to mark a question as duplicate while it's not. – Fr0zenFyr Mar 27 '19 at 12:48
  • Type juggling _is_ the way PHP automatically converts data types, if an operation can not be performed with the initial given ones. You are seeing the effects of it here in your given example. I don’t know what that would still leave open or unexplained … – 04FS Mar 27 '19 at 12:51
  • SoftwareEngineering.SE has [good discussion of it](https://softwareengineering.stackexchange.com/questions/24378/type-casting-variables-in-php-what-is-the-practical-reason-for-doing-this) – Machavity Mar 27 '19 at 12:51
  • @04FS I was expecting "Yes, your guess was correct" or "No, PHP is doing this _... rock 'n' roll..._ behind the scene". – Fr0zenFyr Mar 27 '19 at 12:51
  • And the answer is, PHP is doing type juggling behind the scenes :-p – 04FS Mar 27 '19 at 12:52
  • 1
    @Fr0zenFyr I would hit NikiC or Joe Watkins up in the [PHP room](https://chat.stackoverflow.com/rooms/11/php). Either of them could probably give you an in-depth description of what happens under the hood there – Machavity Mar 27 '19 at 12:54
  • Thanks. That's nice. I'll try chatting with them – Fr0zenFyr Mar 27 '19 at 12:57

0 Answers0