3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Node { public $childs = []; public function __serialize() { return [$this->childs]; } public function __unserialize(array $data) { list($this->childs) = $data; } } function createTree ($width, $depth) { $root = new Node(); $nextLevel = [$root]; for ($level=1; $level<$depth; $level++) { $levelRoots = $nextLevel; $nextLevel = []; while (count($levelRoots) > 0) { $levelRoot = array_shift($levelRoots); for ($w = 0; $w < $width; $w++) { $tester = new Node(); $levelRoot->childs[] = $tester; $nextLevel[] = $tester; } } } return $root; } $width = 3; ob_implicit_flush(); foreach (range(1, 10) as $depth) { $tree = createTree($width, $depth); echo "Testcase tree $width x $depth".PHP_EOL; echo "> Serializing now".PHP_EOL; $serialized = serialize($tree); echo "> Unserializing now".PHP_EOL; $tree = unserialize($serialized); // Lets test whether all is ok! $expectedSize = ($width**$depth - 1)/($width-1); $nodes = [$tree]; $count = 0; while (count($nodes) > 0) { $count++; $node = array_shift($nodes); foreach ($node->childs as $node) { $nodes[] = $node; } } echo "> Unserialized total node count was $count, expected $expectedSize: ".($expectedSize === $count ? 'CORRECT!' : 'INCORRECT'); echo PHP_EOL; echo PHP_EOL; }
Output for 7.1.25 - 7.1.31, 7.2.0 - 7.2.10, 7.2.12 - 7.2.33, 7.3.0 - 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 - 8.3.7
Testcase tree 3 x 1 > Serializing now > Unserializing now > Unserialized total node count was 1, expected 1: CORRECT! Testcase tree 3 x 2 > Serializing now > Unserializing now > Unserialized total node count was 4, expected 4: CORRECT! Testcase tree 3 x 3 > Serializing now > Unserializing now > Unserialized total node count was 13, expected 13: CORRECT! Testcase tree 3 x 4 > Serializing now > Unserializing now > Unserialized total node count was 40, expected 40: CORRECT! Testcase tree 3 x 5 > Serializing now > Unserializing now > Unserialized total node count was 121, expected 121: CORRECT! Testcase tree 3 x 6 > Serializing now > Unserializing now > Unserialized total node count was 364, expected 364: CORRECT! Testcase tree 3 x 7 > Serializing now > Unserializing now > Unserialized total node count was 1093, expected 1093: CORRECT! Testcase tree 3 x 8 > Serializing now > Unserializing now > Unserialized total node count was 3280, expected 3280: CORRECT! Testcase tree 3 x 9 > Serializing now > Unserializing now > Unserialized total node count was 9841, expected 9841: CORRECT! Testcase tree 3 x 10 > Serializing now > Unserializing now > Unserialized total node count was 29524, expected 29524: CORRECT!
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 Testcase tree 3 x 1 > Serializing now > Unserializing now > Unserialized total node count was 1, expected 1: CORRECT! Testcase tree 3 x 2 > Serializing now > Unserializing now > Unserialized total node count was 4, expected 4: CORRECT! Testcase tree 3 x 3 > Serializing now > Unserializing now > Unserialized total node count was 13, expected 13: CORRECT! Testcase tree 3 x 4 > Serializing now > Unserializing now > Unserialized total node count was 40, expected 40: CORRECT! Testcase tree 3 x 5 > Serializing now > Unserializing now > Unserialized total node count was 121, expected 121: CORRECT! Testcase tree 3 x 6 > Serializing now > Unserializing now > Unserialized total node count was 364, expected 364: CORRECT! Testcase tree 3 x 7 > Serializing now > Unserializing now > Unserialized total node count was 1093, expected 1093: CORRECT! Testcase tree 3 x 8 > Serializing now > Unserializing now > Unserialized total node count was 3280, expected 3280: CORRECT! Testcase tree 3 x 9 > Serializing now > Unserializing now > Unserialized total node count was 9841, expected 9841: CORRECT! Testcase tree 3 x 10 > Serializing now > Unserializing now > Unserialized total node count was 29524, expected 29524: CORRECT!
Output for 7.2.11
Testcase tree 3 x 1 > Serializing now > Unserializing now > Unserialized total node count was 1, expected 1: CORRECT! Testcase tree 3 x 2 > Serializing now > Unserializing now > Unserialized total node count was 4, expected 4: CORRECT! Testcase tree 3 x 3 > Serializing now > Unserializing now > Unserialized total node count was 13, expected 13: CORRECT! Testcase tree 3 x 4 > Serializing now > Unserializing now > Unserialized total node count was 40, expected 40: CORRECT! Testcase tree 3 x 5 > Serializing now > Unserializing now > Unserialized total node count was 121, expected 121: CORRECT! Testcase tree 3 x 6 > Serializing now > Unserializing now > Unserialized total node count was 364, expected 364: CORRECT! Testcase tree 3 x 7 > Serializing now > Unserializing now > Unserialized total node count was 1093, expected 1093: CORRECT! Testcase tree 3 x 8 > Serializing now > Unserializing now > Unserialized total node count was 3280, expected 3280: CORRECT! Testcase tree 3 x 9 > Serializing now > Unserializing now > Unserialized total node count was 9841, expected 9841: CORRECT! Testcase tree 3 x 10 > Serializing now > Unserializing now
Process exited with code 137.

preferences:
192.59 ms | 403 KiB | 173 Q