Type Juggling and Conversion
How PHP automatically converts between types, and how to control that conversion explicitly.
2 min read
PHP's dynamic typing means it will often convert a value to a different type automatically, based on context — this is called type juggling. It's convenient until it silently produces a result you didn't expect.
Automatic conversion in comparisons
<?php
var_dump(0 == "abc"); // false in PHP 8+, was true before PHP 8
var_dump("5" == 5); // true — "5" is converted to 5 for comparison
var_dump("5" === 5); // false — different types, no conversion allowed== (loose equality) allows type juggling before comparing; === (strict equality) requires both value and type to match, with no conversion at all. PHP 8 tightened several of =='s more surprising behaviors (like a numeric string compared to 0), but the safest habit — regardless of PHP version — is to default to === and only reach for == when you specifically want type coercion.
Explicit casting
<?php
$input = "42";
$number = (int) $input;
var_dump($number); // int(42)
$total = "19.99";
$float = (float) $total;
var_dump($float); // float(19.99)Casting with (int), (float), (string), (bool), or (array) converts a value explicitly, which is far more predictable than relying on implicit juggling — especially for values coming from outside your code, like form input (which always arrives as strings) or a database result.
Truthiness: what counts as false
<?php
var_dump((bool) ""); // false
var_dump((bool) "0"); // false
var_dump((bool) 0); // false
var_dump((bool) []); // false
var_dump((bool) null); // false
var_dump((bool) "0.0"); // true — only the exact string "0" is falsy
var_dump((bool) "false"); // true — a non-empty string, even this oneThis list — empty string, the string "0", the integer 0, an empty array, and null — are the only values PHP considers falsy. Everything else, including the string "false" (which is a truthy, non-empty string!), evaluates to true. This is one of PHP's most common sources of bugs for developers coming from other languages.
Arithmetic on numeric strings
<?php
$total = "10" + "5";
var_dump($total); // int(15)
$mixed = "10 apples" + 5;PHP converts a numeric-looking string to a number automatically when used with an arithmetic operator. As of PHP 8, a non-numeric leading value like "10 apples" triggers a TypeError rather than silently guessing — a deliberate move toward catching this class of bug earlier.
Strict types: opting out of juggling in function signatures
<?php
declare(strict_types=1);
function addNumbers(int $a, int $b): int {
return $a + $b;
}
addNumbers("5", 3); // TypeError: must be of type int, string givendeclare(strict_types=1) at the top of a file disables automatic type coercion for function arguments and return values in that file — a function typed to accept int will reject a string instead of silently converting it. Most modern PHP style guides recommend enabling this in every file, since it turns a class of silent bugs into an immediate, loud error.
Test what you just learned
4 quick questions. Get all of them right to unlock the next lesson.
You can take the quiz without an account — logging in just lets your result count toward your progress.