PHP Reference ยท Types and Type Handling
PHP type juggling
Reference for PHP type juggling: how values convert automatically in arithmetic, string, comparison and boolean contexts, with PHP 8 rules and examples.
PHP is dynamically typed, so you never declare a variable's type, and PHP converts values automatically when an operation needs a different type. This is called type juggling. Adding "5" to 3 gives 8, using an empty string in an if is false, and comparing 0 == "a" follows rules that changed in PHP 8.
Juggling happens in four main places: arithmetic (operands become numbers), string contexts (. and echo turn values into strings), boolean contexts (if, !, &&), and loose comparison (==, <, switch). Function arguments are also converted unless strict_types is on. You can always make a conversion explicit with a cast.
var_dump("5" + 3); // int(8) var_dump("5" . 3); // string(2) "53" var_dump(0 == "a"); // bool(false) in PHP 8
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
There is no syntax: conversion is implicit. The rules for each context:
| Context | Rule |
|---|---|
Arithmetic + - * / % ** |
Operands become int or float. A numeric string converts. A leading-numeric string such as "12abc" uses its numeric prefix and emits a warning. A string with no numeric prefix, such as "abc", throws a TypeError. Arrays throw a TypeError too. |
| String context | int/float become their decimal text, true becomes "1", false and null become "". Arrays give "Array" with a warning; objects need __toString(). |
| Boolean context | Falsy: false, 0, 0.0, -0.0, "", "0", [], null. Everything else is truthy, including "0.0", " " and "false". |
== with a number and a string |
If the string is numeric, compare as numbers. Otherwise compare the number converted to a string with the string (PHP 8). |
== between two numeric strings |
Compared as numbers ("1e3" == "1000" is true). |
== with null |
null == false, null == 0, null == "", null == [] are true. |
Usage notes
- Use
===by default. It never converts, so1 === "1"is false and surprises disappear. - PHP 8 changed
0 == "a". Before 8.0 it wastrue(the string became0); now it isfalse. Code written for PHP 7 may rely on the old result. - Numeric strings may have leading and trailing whitespace (
" 5","5 ") and are still numeric since PHP 8."5 apples"is only leading-numeric, so arithmetic gives a warning and uses5. Before PHP 8, trailing whitespace made a string non-numeric and fully non-numeric strings only produced a warning and0. - Integer overflow turns into float.
PHP_INT_MAX + 1is a float. - Division returns an
intonly when both operands are integers and the division is exact. - Function arguments juggle too:
strlen(123)works in weak mode butdeclare(strict_types=1)makes it aTypeError. See strict_types. - Array keys are normalized (
"5"becomes5), see Array keys and casting. switchuses==; use match for strict comparison.- Sorting mixed types can give inconsistent results; pass a sort flag or a comparator. See Sort flags and stability.