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 git.master, git.master_jit, rfc.property-hooks
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.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
26.78 ms | 406 KiB | 5 Q