PHP Reference ยท Functions and Classes
PHP intval(), floatval() and strval() functions
Reference for PHP intval(), floatval() and strval(): convert values to int, float and string, how bases and leading numbers work, and how they differ from casts.
intval(), floatval() and strval() convert a value to an integer, a float or a string and return the result. They do the same work as the casts (int), (float) and (string), but as ordinary functions, which makes them handy as callbacks, for example array_map('intval', $list).
Conversion is forgiving: a string such as "42px" becomes 42 because PHP reads the leading number and ignores the rest. Nothing validates the input, so use filter_var() when bad data must be rejected.
var_dump(intval("42px")); // int(42) var_dump(floatval("3.5kg")); // float(3.5) var_dump(strval(12)); // string(2) "12"
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
intval(mixed $value, int $base = 10): int floatval(mixed $value): float // alias: doubleval() strval(mixed $value): string
| Name | Description |
|---|---|
$value |
The value to convert. |
$base |
Optional, intval() only. The base (2 to 36) used when $value is a string. With 0, the base is detected from the prefix: 0x is hexadecimal, 0b binary, a leading 0 octal and anything else decimal. |
Return value: the converted value. Failure never returns an error value: text with no leading number gives 0 or 0.0.
Errors: strval() on an array returns "Array" and raises a warning. strval() on an object without __toString() throws an Error. Passing an object to intval() raises a warning and gives 1.
Usage notes
- Only the leading number counts.
"12abc"is12,"abc12"is0. Leading whitespace is skipped; scientific notation ("1e3") is understood byintval()since PHP 7.1. - Floats truncate toward zero, they do not round:
intval(-42.9)is-42. Useround(),floor()orceil()first when you need a different rule. $baseonly affects strings.intval(42, 8)is still42, because the value is already an integer.- Overflow in strings saturates. A decimal string too large for
PHP_INT_MAXgivesPHP_INT_MAX. A float outside the integer range has no reliable result, so check the range yourself. NANandINFbecome0inintval().- Booleans and null:
trueis1,falseandnullare0; forstrval(),trueis"1"andfalseandnullare"". - Casts do the same work:
(int) $x,(float) $x,(string) $x. The functions win when a callback is needed. - Floats become strings using the
precisionini setting (14 significant digits by default), sostrval(0.1 + 0.2)is"0.3"andstrval(1.50)is"1.5".var_export()andjson_encode()useserialize_precisioninstead. - Money: do not store prices as floats; keep integer cents and convert with
intval()only at the edges.