3

I got problem with this code:

if (!empty($_GET[ "lic" ])) $lic = $_GET[ "lic" ]; else $e = true;
echo ($lic % 11);

When I post 8911076856 it echoes 1, but it should be 0.

Florent
  • 12,310
  • 10
  • 49
  • 58
Ondro Tadanai
  • 123
  • 2
  • 12
  • 4
    Might that be because `8911076856` is too high for an int, and overflowing? – andrewsi Aug 22 '12 at 14:03
  • I think that's not the problem as `var_dump(8911076856 % 11);` returns `int(0)`. Call `var_dump($lic)` before echoing the modulo. – Florent Aug 22 '12 at 14:06
  • 1
    @Florent - that is the problem. It's a 32bit system and he's trying to handle value too large to fit 4 byte integer. You probably tested on 64 bit system. – N.B. Aug 22 '12 at 14:08
  • @Florent: In that operation, the number `8911076856` is parsed as a `float`, but cast internally to an `integer` for the modulus. Try `var_dump((int)8911076856);` to see what it's actually using in the operation. – FtDRbwLXw6 Aug 22 '12 at 14:09
  • probably its 32b system, i dont know it exactly (Apache/2.2.15 (Debian) Server), but if it doesnt work, than is easy to find out – Ondro Tadanai Aug 22 '12 at 16:43
  • but is there any greater numeric variable than int that can be used in php? if yes how to declare that type of variable? – Ondro Tadanai Aug 22 '12 at 16:44
  • and theres another problem, when i insert this value (8911076856) into mysql db it wont save it, it save 2147483647 insted. I change mysql column type to bigint (and float as well), but with no success... how to save it ?(modulo now works with fmod function) – Ondro Tadanai Aug 22 '12 at 17:10

4 Answers4

9

The value "8911076856" is probably above the maximum integer value of your system.

echo ((int)8911076856);

My result is 321142264 on my 32 Bit system.

oopbase
  • 11,157
  • 12
  • 40
  • 59
  • That's right, I got the right result (`0`) on a 64bits system – Touki Aug 22 '12 at 14:06
  • In this case, PHP automatically converts the number to a float. Apparently, the modulo operator does not work correctly on floats. – Sjoerd Aug 22 '12 at 14:07
7

Use fmod:

echo fmod(8911076856, 11);
Sjoerd
  • 74,049
  • 16
  • 131
  • 175
  • +1. Out of curiosity, is the benefit of `fmod()` over `(float)$x % (float)$y` just the precision of the result? – FtDRbwLXw6 Aug 22 '12 at 14:12
  • No, `(float)$x % (float)$y` does not give the correct result for values of $x above PHP_INT_MAX. E.g. `(float)8911076856 % (float)10` gives 4 instead of 6. Apparently the modulo operator casts both arguments back to int. – Sjoerd Aug 22 '12 at 14:19
2

This is most likely being caused because the number you're posting is higher than PHP_INT_MAX, which is 9223372036854775807 on most 64-bit systems AFAIK. If you're using a 32-bit system (which I expect you are), it's probably 2147483647.

FtDRbwLXw6
  • 27,774
  • 13
  • 70
  • 107
-2

Did you tried this:

if (!empty($_GET[ "lic" ])) $lic = intval($_GET[ "lic" ]); else $e = true;

echo ($lic % 11);

Eliane
  • 1
  • 1
  • This won't work. Intval returns an int, and 8911076856 does not fit in an int. I.e. `intval(8911076856)` will give 321142264 on 32-bit machines. – Sjoerd Aug 22 '12 at 14:41