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

The easiest way to convert strings to numbers in PHP is to use the (int/float/double)$variable cast, which directly converts the string to a number. You can also convert a string to a number using the intval(), floatval(), number_format() and settype() functions. The intval() function converts a string to an integer, and the floatval() function converts a string to a floating-point value. The number_format() function converts a string to a number and returns the formatted number on success; if it fails, the function throws an "E_WARNING" error. The settype() function is used to set or change the type of an existing variable. In this PHP String to Int example, we convert a string to a number using the intval() function. Below you can see more PHP string to int conversion examples with detailed descriptions of each method. Click Execute to run the PHP String to Int Example online and see the result.
Loading...