devnotes.

Type Casting and Conversion

PHP performs a lot of implicit (automatic) type conversion, especially with the loose comparison operators and string/number mixing. This flexibility is convenient but can also cause subtle bugs if you're not aware of it.

$total = "5" + 3;      // 8 (string "5" is converted to int)
$msg = "Count: " . 5;   // "Count: 5" (int 5 is converted to string)

if ("0" == false) { /* true — loose comparison */ }
if ("0" === false) { /* false — strict comparison, different types */ }

Explicit casting uses a type name in parentheses:

$str = "42";
$num = (int) $str;      // 42 (int)
$float = (float) "3.14"; // 3.14
$bool = (bool) "";       // false (empty string is falsy)
$arr = (array) "hello";  // ["hello"]

Falsy values in PHP (values that evaluate to false in a boolean context): false, 0, 0.0, "", "0", [], and null. Everything else is truthy — including the string "0.0" (which is truthy, a common gotcha!).

Because of these subtleties, modern PHP code strongly favors strict comparisons (===, !==) over loose ones (==, !=), and you can enable declare(strict_types=1); at the top of a file to make function type hints strict instead of coercive.

PHP Type Casting and Loose vs Strict Comparison | DevNotes