PHP Type Juggling and Casting
PHP converts types automatically. Cast with (int), (float), (string), (bool) and (array), or use intval and floatval. declare(strict_types=1) makes type errors strict.
Example
var_dump((int) "42abc");
var_dump((float) "3.5");
var_dump((bool) "0");
var_dump((string) 12);
var_dump("10" + 5);
var_dump(0 == "a");
var_dump("1" == "01");Result
int(42) float(3.5) bool(false) string(2) "12" int(15) bool(false) bool(true)
Exercise
Cast to integer.
$n = () "5";
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.