3v4l.org

run code in 300+ PHP versions simultaneously
<?php function test_type(mixed $value, string $type) { if (in_array(($actual = gettype($value)), ['string', 'integer', 'NULL'])) { if (str_contains($type, '?') && $actual === 'NULL') return true; return $actual === $type; } return in_array(true, array_map(fn(string $x) => $value instanceof $x, explode('|', $type))); } echo (int) test_type(null, "int"); echo (int) test_type(null, "?int"); echo (int) test_type("hello, world", "bool"); echo (int) test_type(new DateTime, "DateTimeInterface"); echo (int) test_type(new DateTime, "null|int|DateTime|DateTimeImmutable");
Output for 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
01011

preferences:
62.45 ms | 979 KiB | 4 Q