Interview frequency: …
Short answer
Type Juggling automatically converts types implicitly, while strict_types requires exact types for parameters and return values and throws TypeError on a mismatch.
Explanation
By default in PHP, if a function expects an int and you pass the string '42', the language converts it implicitly. When declare(strict_types=1) is placed on the first line of a file, implicit conversions are disabled for calls made from that file, and the engine immediately throws TypeError. This prevents hidden errors and improves code safety and stability.
Code example
<?php
declare(strict_types=1);
function add(int $a, int $b): int {
return $a + $b;
}
add(5, 10); // True
// add("5", 10); // Throws Fatal TypeErrorCommon mistakes
- Believing that declare(strict_types=1) affects the whole project when placed in one file; it applies only to the file where it is written.