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 ]; function ignoreCount($data) { return @count($data); } function validatedCount($data) { if (PHP_VERSION_ID >= 70300) { if (is_countable($data)) { return count($data); } } elseif (PHP_VERSION_ID >= 70100) { if (is_array($data) || $data instanceof \Countable) { return count($data); } elseif(is_iterable($data)) { return iterator_count($data); } } else { if (is_array($data) || $data instanceof \Countable) { return count($data); } elseif ($data instanceof \Traversable) { return iterator_count($data); } } return null === $data ? 0 : 1; } function castCount($data) { return count((array) $data); } foreach ($tests as $type => $data) { $expected = @count($data); $normal = count($data); $ignored = ignoreCount($data); $validated = validatedCount($data); $casted = castCount($data); echo 'Type ' . $type . ': ' . \PHP_EOL; echo "\t" . 'count(): ' . print_r($normal, 1) . ' '; var_dump($normal === $expected); echo "\t" . 'Ignored: ' . print_r($ignored, 1) . ' '; var_dump($ignored === $expected); echo "\t" . 'Validated: ' . print_r($validated, 1) . ' '; var_dump($validated === $expected); echo "\t" . 'Casted: ' . print_r($casted, 1) . ' '; var_dump($casted === $expected); echo \PHP_EOL . '------' . \PHP_EOL . \PHP_EOL; }
Output for 8.1.0 - 8.1.29, 8.2.0 - 8.2.23, 8.3.0 - 8.3.11
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/UFieo on line 14 Type array: count(): 3 bool(true) Ignored: 3 bool(true) Validated: 3 bool(true) Casted: 3 bool(true) ------ Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, string given in /in/UFieo:71 Stack trace: #0 {main} thrown in /in/UFieo on line 71
Process exited with code 255.
Output for 8.0.10 - 8.0.30
Type array: count(): 3 bool(true) Ignored: 3 bool(true) Validated: 3 bool(true) Casted: 3 bool(true) ------ Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, string given in /in/UFieo:71 Stack trace: #0 {main} thrown in /in/UFieo on line 71
Process exited with code 255.
Output for 8.0.0 - 8.0.9
Type array: count(): 3 bool(true) Ignored: 3 bool(true) Validated: 3 bool(true) Casted: 3 bool(true) ------ Fatal error: Uncaught TypeError: count(): Argument #1 ($var) must be of type Countable|array, string given in /in/UFieo:71 Stack trace: #0 {main} thrown in /in/UFieo on line 71
Process exited with code 255.
Output for 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33
Type array: count(): 3 bool(true) Ignored: 3 bool(true) Validated: 3 bool(true) Casted: 3 bool(true) ------ Warning: count(): Parameter must be an array or an object that implements Countable in /in/UFieo on line 72 Type string: count(): 1 bool(true) Ignored: 1 bool(true) Validated: 1 bool(true) Casted: 1 bool(true) ------ Warning: count(): Parameter must be an array or an object that implements Countable in /in/UFieo on line 72 Type number: count(): 1 bool(true) Ignored: 1 bool(true) Validated: 1 bool(true) Casted: 1 bool(true) ------ Type iterator: count(): 3 bool(true) Ignored: 3 bool(true) Validated: 3 bool(true) Casted: 3 bool(true) ------ Type countable: count(): 3 bool(true) Ignored: 3 bool(true) Validated: 3 bool(true) Casted: 1 bool(false) ------ Warning: count(): Parameter must be an array or an object that implements Countable in /in/UFieo on line 72 Type zero: count(): 1 bool(true) Ignored: 1 bool(true) Validated: 1 bool(true) Casted: 1 bool(true) ------ Warning: count(): Parameter must be an array or an object that implements Countable in /in/UFieo on line 72 Type string_zero: count(): 1 bool(true) Ignored: 1 bool(true) Validated: 1 bool(true) Casted: 1 bool(true) ------ Warning: count(): Parameter must be an array or an object that implements Countable in /in/UFieo on line 72 Type object: count(): 1 bool(true) Ignored: 1 bool(true) Validated: 1 bool(true) Casted: 3 bool(false) ------ Warning: count(): Parameter must be an array or an object that implements Countable in /in/UFieo on line 72 Type stdClass: count(): 1 bool(true) Ignored: 1 bool(true) Validated: 1 bool(true) Casted: 0 bool(false) ------ Warning: count(): Parameter must be an array or an object that implements Countable in /in/UFieo on line 72 Type null: count(): 0 bool(true) Ignored: 0 bool(true) Validated: 0 bool(true) Casted: 0 bool(true) ------ Warning: count(): Parameter must be an array or an object that implements Countable in /in/UFieo on line 72 Type empty: count(): 1 bool(true) Ignored: 1 bool(true) Validated: 1 bool(true) Casted: 1 bool(true) ------ Warning: count(): Parameter must be an array or an object that implements Countable in /in/UFieo on line 72 Type boolt: count(): 1 bool(true) Ignored: 1 bool(true) Validated: 1 bool(true) Casted: 1 bool(true) ------ Warning: count(): Parameter must be an array or an object that implements Countable in /in/UFieo on line 72 Type boolf: count(): 1 bool(true) Ignored: 1 bool(true) Validated: 1 bool(true) Casted: 1 bool(true) ------
Output for 7.1.0 - 7.1.21
Type array: count(): 3 bool(true) Ignored: 3 bool(true) Validated: 3 bool(true) Casted: 3 bool(true) ------ Type string: count(): 1 bool(true) Ignored: 1 bool(true) Validated: 1 bool(true) Casted: 1 bool(true) ------ Type number: count(): 1 bool(true) Ignored: 1 bool(true) Validated: 1 bool(true) Casted: 1 bool(true) ------ Type iterator: count(): 3 bool(true) Ignored: 3 bool(true) Validated: 3 bool(true) Casted: 3 bool(true) ------ Type countable: count(): 3 bool(true) Ignored: 3 bool(true) Validated: 3 bool(true) Casted: 1 bool(false) ------ Type zero: count(): 1 bool(true) Ignored: 1 bool(true) Validated: 1 bool(true) Casted: 1 bool(true) ------ Type string_zero: count(): 1 bool(true) Ignored: 1 bool(true) Validated: 1 bool(true) Casted: 1 bool(true) ------ Type object: count(): 1 bool(true) Ignored: 1 bool(true) Validated: 1 bool(true) Casted: 3 bool(false) ------ Type stdClass: count(): 1 bool(true) Ignored: 1 bool(true) Validated: 1 bool(true) Casted: 0 bool(false) ------ Type null: count(): 0 bool(true) Ignored: 0 bool(true) Validated: 0 bool(true) Casted: 0 bool(true) ------ Type empty: count(): 1 bool(true) Ignored: 1 bool(true) Validated: 1 bool(true) Casted: 1 bool(true) ------ Type boolt: count(): 1 bool(true) Ignored: 1 bool(true) Validated: 1 bool(true) Casted: 1 bool(true) ------ Type boolf: count(): 1 bool(true) Ignored: 1 bool(true) Validated: 1 bool(true) Casted: 1 bool(true) ------

preferences:
76.72 ms | 419 KiB | 5 Q