How do I convert a string to a number in PHP?

Created by decezeLinked to 61m issues across 99 teams

tl;dr

In PHP, you don't usually need to explicitly convert a string to a number, as the language will do this for you in most cases. However, if you do need to convert a string to a number, you can use the cast method.

$num = "3.14"; $int = (int)$num; $float = (float)$num;

That's it! You've successfully converted a string to a number in PHP.

We use cookies to personalize content
run ads, and analyze traffic.