3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace wrossmann\merkle; class Node { public $value = NULL; public $isLeaf = false; public $height = 0; public $left = NULL; public $right = NULL; public $parent = NULL; } class Tree { protected $root = null; protected $nodes = []; public function serialize() { yield from $this->serializeGenerator($this->root); } // Serialize based on a depth-first traversal protected function serializeGenerator(Node $node) { yield [ 'v' => $node->value, 'l' => $node->isLeaf ]; if( ! is_null($node->left) ) { yield from $this->serializeGenerator($node->left); } if( ! is_null($node->right) ) { yield from $this->serializeGenerator($node->right); } } } // pre-constructed tree $m = unserialize(gzuncompress(base64_decode('eJy1VbtuwzAMzLdw7GTLz1K/UDhLxyxKIj9Qxy6kPoYg/17SXmIoKBBI2kj6rDvyaHmPIkX4NbO1FzVNh4s2H6M+vButAQVeLVYIu5edmecvkPvH6GY+E7pkdIHwo8ZvDdJijaCOp7Nuu57TEmGwb1q1II+YrIVeD11PBw9LIUcYdfs0T77yROUQzBGVgaDKIUj/JWjkcoRZHzYr9lMZPVFqME3k7R7wtB634QB6NqXSRyF5Etl1gp7CzkC8enriNhxAj+vJFuSjmZREvwEEc8TeBB128kXmuQluwwH0bEq517KSKZFtJ2gXdgiV16oT1G04gJ4Hprjf512l4bxe/tsTKbUgFaVXpqZbOCcNKb+VUiAoyDIKMj46oSDnXagoKPguYHDJQkoKKgpqWpPbH0CgjUU='))); // only outputs the final element printf("%s\n\n", json_encode(iterator_to_array($m->serialize()))); // produces all elemenst as expected foreach($m->serialize() as $item) { printf("%s\n", json_encode($item)); }
Output for 7.1.0 - 7.1.24, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
[{"v":"h","l":true}] {"v":"abcdefgh","l":false} {"v":"abcd","l":false} {"v":"ab","l":false} {"v":"a","l":true} {"v":"b","l":true} {"v":"cd","l":false} {"v":"c","l":true} {"v":"d","l":true} {"v":"efgh","l":false} {"v":"ef","l":false} {"v":"e","l":true} {"v":"f","l":true} {"v":"gh","l":false} {"v":"g","l":true} {"v":"h","l":true}
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 [{"v":"h","l":true}] {"v":"abcdefgh","l":false} {"v":"abcd","l":false} {"v":"ab","l":false} {"v":"a","l":true} {"v":"b","l":true} {"v":"cd","l":false} {"v":"c","l":true} {"v":"d","l":true} {"v":"efgh","l":false} {"v":"ef","l":false} {"v":"e","l":true} {"v":"f","l":true} {"v":"gh","l":false} {"v":"g","l":true} {"v":"h","l":true}
Output for 5.6.38
Parse error: syntax error, unexpected '$this' (T_VARIABLE) in /in/LBeA4 on line 18
Process exited with code 255.

preferences:
172.21 ms | 401 KiB | 175 Q