3v4l.org

run code in 300+ PHP versions simultaneously
<?php function dirToArray1($dir) { $result = array(); $cdir = scandir($dir); foreach ($cdir as $key => $value) { if (!in_array($value,array(".",".."))) { if (is_dir($dir . DIRECTORY_SEPARATOR . $value)){ $result[$value] = dirToArray1($dir . DIRECTORY_SEPARATOR . $value); } else { $result[] = $value; } } } return $result; } function dirToArray2($dir) { $arr = []; $obj = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::CHILD_FIRST); foreach ($obj as $info) { if (in_array($info->getFilename(), ['.', '..'])) continue; $path = $info->isDir() ? [$info->getFilename() => []] : [$info->getFilename()]; for ($depth = $obj->getDepth() - 1; $depth >= 0; $depth--) { $path = [$obj->getSubIterator($depth)->current()->getFilename() => $path]; } $arr = array_merge_recursive($arr, $path); } return $arr; } foreach (['2'] as $k) { $t = "t$k"; $d = "d$k"; $$t = microtime(true); $func = "dirToArray$k"; $$d = $func("/"); $$t = microtime(true) - $$t; echo "$func needed " . $$t . " time\n"; }
Output for 8.0.0 - 8.0.12
Fatal error: Uncaught UnexpectedValueException: RecursiveDirectoryIterator::__construct(//run/systemd/incoming): Failed to open directory: Permission denied in /in/82N64:21 Stack trace: #0 [internal function]: RecursiveDirectoryIterator->__construct('//run/systemd/i...', 0) #1 /in/82N64(21): RecursiveDirectoryIterator->getChildren() #2 /in/82N64(37): dirToArray2('/') #3 {main} thrown in /in/82N64 on line 21
Process exited with code 255.
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.31, 7.4.0 - 7.4.25
Fatal error: Uncaught UnexpectedValueException: RecursiveDirectoryIterator::__construct(//run/systemd/incoming): failed to open dir: Permission denied in /in/82N64:21 Stack trace: #0 [internal function]: RecursiveDirectoryIterator->__construct('//run/systemd/i...', 0) #1 /in/82N64(21): RecursiveDirectoryIterator->getChildren() #2 /in/82N64(37): dirToArray2('/') #3 {main} thrown in /in/82N64 on line 21
Process exited with code 255.
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
Fatal error: Uncaught exception 'UnexpectedValueException' with message 'RecursiveDirectoryIterator::__construct(//run/systemd/incoming): failed to open dir: Permission denied' in /in/82N64:21 Stack trace: #0 [internal function]: RecursiveDirectoryIterator->__construct('//run/systemd/i...', 0) #1 /in/82N64(21): RecursiveDirectoryIterator->getChildren() #2 /in/82N64(37): dirToArray2('/') #3 {main} thrown in /in/82N64 on line 21
Process exited with code 255.
Output for 5.2.3 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/82N64 on line 19
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.2
<br /> <b>Parse error</b>: syntax error, unexpected '[' in <b>/in/82N64</b> on line <b>19</b><br />
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
<br /> <b>Parse error</b>: parse error, unexpected '[' in <b>/in/82N64</b> on line <b>19</b><br />
Process exited with code 255.
Output for 4.3.2 - 4.3.4
<br /> <b>Parse error</b>: parse error in <b>/in/82N64</b> on line <b>19</b><br />
Process exited with code 255.

preferences:
217.84 ms | 401 KiB | 386 Q