14

I've got some strings that I need to cast as floats but have seen two apparently different ways of doing it:

$float = floatval ($float);

and

$float = (float) $float;

Are there any differences between the two methods?

James Kerslake
  • 171
  • 1
  • 2
  • 7

1 Answers1

0

In all other cases it will be evaluated as a float. In other words, the $string is first interpreted as INT, which cause overflow (The $string value 2968789218 exceeds the maximum value ( PHP_INT_MAX ) of 32-bit PHP, which is 2147483647.), then evaluated to float by (float) or floatval()

Please have look at PHP Convert String into Float/Double

The best answer for this is Typecasting vs function to convert variable type in PHP

Community
  • 1
  • 1
Pranav MS
  • 2,235
  • 2
  • 23
  • 50
  • 8
    interesting information, however it **does not answer** the question in any way. should have been a comment. (also, since it only occurs on 32-bit installations of PHP, it probably only affects a small percentage of systems.) – Franz Gleichmann Dec 06 '16 at 12:36