3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Foo { private $a; protected $b; public $c; } class FooCountable implements \Countable { private $elements = [1,2,3]; public function count() { return count($this->elements); } } $tests = [ 'array' => [1,2,3], 'string' => 'hello world', 'number' => 123, 'iterator' => new \ArrayIterator([1,2,3]), 'countable' => new \FooCountable(), 'zero' => 0, 'string_zero' => '0', 'object' => new \Foo(), 'stdClass' => new \stdClass, 'null' => null, 'empty' => '', 'boolt' => true, 'boolf' => false ]; if (!\function_exists('is_countable')) { function is_countable($value) { echo 'used shim '; return \is_array($value) || $value instanceof \Countable; } } function countValid($array_or_countable, $mode = \COUNT_NORMAL) { if (\is_countable($array_or_countable)) { return \count($array_or_countable, $mode); } return null === $array_or_countable ? 0 : 1; } foreach($tests as $key => $test) { echo $key . ': ' . print_r(countValid($test), 1) . \PHP_EOL; }
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Deprecated: Return type of FooCountable::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/i5KWH on line 14 array: 3 string: 1 number: 1 iterator: 3 countable: 3 zero: 1 string_zero: 1 object: 1 stdClass: 1 null: 0 empty: 1 boolt: 1 boolf: 1
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 Deprecated: Return type of FooCountable::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/i5KWH on line 14 array: 3 string: 1 number: 1 iterator: 3 countable: 3 zero: 1 string_zero: 1 object: 1 stdClass: 1 null: 0 empty: 1 boolt: 1 boolf: 1
Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
array: 3 string: 1 number: 1 iterator: 3 countable: 3 zero: 1 string_zero: 1 object: 1 stdClass: 1 null: 0 empty: 1 boolt: 1 boolf: 1
Output for 5.6.38 - 5.6.40, 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33
used shim array: 3 used shim string: 1 used shim number: 1 used shim iterator: 3 used shim countable: 3 used shim zero: 1 used shim string_zero: 1 used shim object: 1 used shim stdClass: 1 used shim null: 0 used shim empty: 1 used shim boolt: 1 used shim boolf: 1

preferences:
187.35 ms | 402 KiB | 216 Q