Is it safe to use INT-type variables in BCMath functions in PHP ?
Example:
<?php
$a = 1;
$b = "1";
echo bcadd($a,$b,0);
?>
This seems to work but is it safe to do this ? Or is there for example a risk that PHP can interpret an INT as something else than it's int value (I'm thinking about hexadecimal values etc) ?
Thanks !
Be careful with very large numbers. If you assign a literal integer > PHP_INT_MAX
to a variable, PHP will automatically convert it to a float. I know you specifically asked about ints, and in that case you'd be passing a float, but if you don't know that about the automatic conversion it looks like you're using a large int, so I thought it worth mentioning.
$a = 9223372036854775808;
$b = '1';
var_dump($b); // float 9.2233720368548E+18
echo bcadd($a, $b, 0); // echoes 1
Basically, the function does take a string. If you give it something that isn't a string, PHP will automatically convert it to a string if it can, unless you have enabled strict mode.