Originally published on zero2php.dev — part of the Zero to PHP course. View this lesson on the original site.
Explicit casts
You can convert with casts: (int), (float), (string), (bool), (array), (object).
<?php
declare(strict_types=1);
$x = (int) '42';
var_dump($x);
Equality and juggling
PHP also juggles types in some operations—== can surprise you. When you care about both value and type, prefer === and !==.
declare(strict_types=1); tightens type checks for function arguments/returns once you define functions in Chapter 2—keep it in new files now as a good habit.
Exercise
echo the integer 42 by casting: use (int) on the string '42'.
From Zero to PHP — PHP foundations: environment, syntax, and values