0

For learning experience, I want to see the maximum integer representable by PHP and whether or not it will err if the range limit is exceeded. I tried this , but it just prints the sum well.

PHP_INT_MAX + PHP_INT_MAX // outputs 4294967294.0

// expected warning here, but nothing happened. result returned good.

If PHP_INT_MAX is the maximum integer php can handle, how come we can exceed it on our expressions?

  • It's turned into a float. Please show your complete code. – KIKO Software Oct 03 '17 at 13:44
  • iirc everything that is larger then `PHP_INT_MAX` is represented as float. – t.niese Oct 03 '17 at 13:45
  • @KIKOSoftware you might be right. I tried `gettype(PHP_INT_MAX + PHP_INT_MAX)` it prints `double` – Cholthi Paul Ttiopic Oct 03 '17 at 13:46
  • `pow(PHP_INT_MAX, PHP_INT_MAX)` :) – brevis Oct 03 '17 at 13:47
  • @brevis No bomb exploded. Looks like PHP is flexible when it comes to printing – Cholthi Paul Ttiopic Oct 03 '17 at 13:53
  • @CholthiPaulTtiopic it's dynamic typed which means it changes the type of the variable to whatever it needs to. A variable which was an integer in line 3 can be a float in line 4 and even a string in line 5. That might be the problem. – Nico Oct 03 '17 at 13:56
  • @t-niese This might be a duplicate question *(I couldn't actually find a duplicate question with a quick search)*, but it's not the same as the linked question. This one is asking about why you can't overflow the maximum integer, and the other one is asking which is the maximum integer – Piyin Oct 03 '17 at 14:12
  • @Piyin You're right that the question is different and therefore not necessarily a duplicate. But that's not the only criterion for a duplicate. If the problem and the answers to a different question fit, then it can still be considered a duplicate. And you could copy the [answer](https://stackoverflow.com/a/2842548/1960455) to this question, and it would match perfectly fine. – t.niese Oct 05 '17 at 05:08
  • @t-niese While I agree one could actually find the information among the answers in another question, sometimes one won't even bother checking the question because the title itself doesn't sound like it's going to answer one's question. In this case, though, it isn't really the case. That answer answers this, but I bet someone could focus on this question and provide a more detailed answer. It's just something to think about. Anyway, if you consider that's the answer, you should probably answer this one with a link to the other one, as a community answer, so people could find it easily here – Piyin Oct 05 '17 at 14:13

0 Answers0