PHP Reference ยท Types and Type Handling
PHP type casting
Reference for PHP type casting: the (int), (float), (string), (bool), (array) and (object) casts, conversion rules, intval() and settype(), and pitfalls.
A cast converts a value to another type on purpose. You write the target type in parentheses before the value: (int) "42". Casting is the explicit counterpart of type juggling, which PHP does on its own.
The canonical cast operators are (int), (float), (string), (bool), (array) and (object). Older spellings such as (integer), (double) and (boolean) still work but are deprecated in PHP 8.5, so avoid them. The (real) and (unset) casts were removed in PHP 8.0.
var_dump((int) "42abc"); // int(42) var_dump((bool) "0"); // bool(false) var_dump((array) "text"); // array(1) { [0]=> "text" }
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
(int) $value (float) $value (string) $value (bool) $value (array) $value (object) $value intval($value, $base = 10) floatval($value) strval($value) boolval($value) settype(mixed &$var, string $type): bool
| Cast | Main rules |
|---|---|
(int) |
Floats truncate toward zero. Strings use the numeric prefix (or 0). true is 1, null is 0. NAN and infinity give 0 (PHP 8.5 warns that the float is not representable); other floats outside the integer range have no reliable result. |
(float) |
Strings use the numeric prefix (including exponent). |
(string) |
Numbers become decimal text; true is "1"; false and null are ""; arrays give "Array" with a warning; objects need __toString(). |
(bool) |
See the falsy list in bool. |
(array) |
Scalars become a one-element list, null becomes [], objects become property arrays. |
(object) |
Arrays become stdClass with keys as properties; scalars become {scalar: value}; null becomes an empty object. |
Usage notes
- Casts never throw for scalars.
(int) "abc"quietly gives0. When you need validation, usefilter_var()oris_numeric()first. - Strings are cast from the left.
(int) "12abc"is 12, but(int) "abc12"is 0. Hex ("0x1A") and octal-looking ("012") strings are decimal when cast with(int);intval()with base0or16interprets prefixes. - Float to int truncates, it does not round. Use
round(),floor()orceil()first when needed. Only the explicit cast is silent: since PHP 8.1, an implicit conversion that loses a fraction (such as using1.5as an array key) is deprecated. - Objects to arrays include private and protected properties with special names containing NUL bytes, so the array has the same count as properties but unusual keys.
(string)of floats uses theprecisionini setting rules (shortest round-trip representation since PHP 8:(string) 0.1is"0.1").(unset)was removed in PHP 8.0. Assignnullor useunset($var).settype()converts a variable in place and returnstrueorfalse; the cast operators andintval()return a new value instead and are easier to read.- Objects cannot become numbers.
(int) $objectwarns and returns1. - Strict mode does not affect casts.
declare(strict_types=1)only changes how arguments and return values are checked, never an explicit cast.