Typecasting vs function to convert variable type in PHP

There’s no difference in the resulting value, just:

  • (float) is a language feature and very quick
  • floatval() incurs the overhead of a function call (minimal, but nonetheless…)
  • floatval() as a function can be used in ways that (float) cannot, e.g. array_map('floatval', $foo)

The last point is, I believe, the main reason for floatval‘s existence: so each casting operation has a function equivalent, which can be useful in some circumstances.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)