<?php
declare(strict_types=1);
function acceptBool(bool $x) {}
function acceptInt(int $x) {}
function acceptFloat(float $x) {}
function acceptString(string $x) {}
function acceptArray(array $x) {}
function acceptObject(object $x) {}
$functions = [
"bool" => "acceptBool",
"int" => "acceptInt",
"float" => "acceptFloat",
"string" => "acceptString",
"array" => "acceptArray",
"object" => "acceptObject",
];
$vars = [
"null" => null,
"bool" => true,
"int" => 1,
"float" => 3.14,
"numeric string" => "2.72",
"non-numeric string" => "elephphant",
"array" => [1, 2, 3],
"object" => new stdClass(),
];
foreach ($vars as $passedType => $var) {
foreach ($functions as $acceptedType => $function) {
if ($passedType === $acceptedType) {
continue;
}
echo str_pad($passedType, 18) . " -> " . str_pad($acceptedType, 10) . ": ";
try {
$function($var);
echo "success";
} catch(TypeError $e) {
echo "failure";
}
echo "\n";
}
}
- Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.30, 8.2.0 - 8.2.25, 8.3.0 - 8.3.4, 8.3.6 - 8.3.13
- null -> bool : failure
null -> int : failure
null -> float : failure
null -> string : failure
null -> array : failure
null -> object : failure
bool -> int : failure
bool -> float : failure
bool -> string : failure
bool -> array : failure
bool -> object : failure
int -> bool : failure
int -> float : success
int -> string : failure
int -> array : failure
int -> object : failure
float -> bool : failure
float -> int : failure
float -> string : failure
float -> array : failure
float -> object : failure
numeric string -> bool : failure
numeric string -> int : failure
numeric string -> float : failure
numeric string -> string : success
numeric string -> array : failure
numeric string -> object : failure
non-numeric string -> bool : failure
non-numeric string -> int : failure
non-numeric string -> float : failure
non-numeric string -> string : success
non-numeric string -> array : failure
non-numeric string -> object : failure
array -> bool : failure
array -> int : failure
array -> float : failure
array -> string : failure
array -> object : failure
object -> bool : failure
object -> int : failure
object -> float : failure
object -> string : failure
object -> array : failure
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
null -> bool : failure
null -> int : failure
null -> float : failure
null -> string : failure
null -> array : failure
null -> object : failure
bool -> int : failure
bool -> float : failure
bool -> string : failure
bool -> array : failure
bool -> object : failure
int -> bool : failure
int -> float : success
int -> string : failure
int -> array : failure
int -> object : failure
float -> bool : failure
float -> int : failure
float -> string : failure
float -> array : failure
float -> object : failure
numeric string -> bool : failure
numeric string -> int : failure
numeric string -> float : failure
numeric string -> string : success
numeric string -> array : failure
numeric string -> object : failure
non-numeric string -> bool : failure
non-numeric string -> int : failure
non-numeric string -> float : failure
non-numeric string -> string : success
non-numeric string -> array : failure
non-numeric string -> object : failure
array -> bool : failure
array -> int : failure
array -> float : failure
array -> string : failure
array -> object : failure
object -> bool : failure
object -> int : failure
object -> float : failure
object -> string : failure
object -> array : failure
preferences:
66.79 ms | 418 KiB | 5 Q