Skip to content
Tutorials Center
My learning

Topics

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.

PHP
var_dump(intval("42px"));    // int(42)
var_dump(floatval("3.5kg")); // float(3.5)
var_dump(strval(12));        // string(2) "12"

Try it

PHP Converting strings and floats

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.

PHP The base argument of intval()

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

PHP
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" is 12, "abc12" is 0. Leading whitespace is skipped; scientific notation ("1e3") is understood by intval() since PHP 7.1.
  • Floats truncate toward zero, they do not round: intval(-42.9) is -42. Use round(), floor() or ceil() first when you need a different rule.
  • $base only affects strings. intval(42, 8) is still 42, because the value is already an integer.
  • Overflow in strings saturates. A decimal string too large for PHP_INT_MAX gives PHP_INT_MAX. A float outside the integer range has no reliable result, so check the range yourself.
  • NAN and INF become 0 in intval().
  • Booleans and null: true is 1, false and null are 0; for strval(), true is "1" and false and null are "".
  • Casts do the same work: (int) $x, (float) $x, (string) $x. The functions win when a callback is needed.
  • Floats become strings using the precision ini setting (14 significant digits by default), so strval(0.1 + 0.2) is "0.3" and strval(1.50) is "1.5". var_export() and json_encode() use serialize_precision instead.
  • Money: do not store prices as floats; keep integer cents and convert with intval() only at the edges.