3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Test { protected $path = '/tmp/directory'; public function createTestFiles () { $amount = 63; if (!is_dir($this->path)) { mkdir($this->path); } foreach (scandir($this->path) as $filename) { @unlink("{$this->path}/$filename"); } for ($num = 1; $num <= $amount; $num++) { fopen("{$this->path}/{$num}.txt", "a"); } } public function runTest() { echo 'get by foreach: ' . $this->getByForeach() . PHP_EOL; echo 'get by foreach with rewind: ' . $this->getByForeachWithRewind() . PHP_EOL; echo 'get by foreach after foreach: ' . $this->getByForeachAfterForeach() . PHP_EOL; echo 'get by loop: ' . $this->getByloop() . PHP_EOL; echo 'get by iterator_to_array: ' . $this->getByIteratorToArray() . PHP_EOL; echo 'get by iterator_to_array with rewind: ' . $this->getByIteratorToArrayWithRewind() . PHP_EOL; } public function getByForeach() { $directory = new RecursiveDirectoryIterator($this->path); $count = 0; foreach ($directory as $filename => $fileInfo) { $count++; } return $count; } public function getByForeachWithRewind() { $directory = new RecursiveDirectoryIterator($this->path); $count = 0; $directory->rewind(); foreach ($directory as $filename => $fileInfo) { $count++; } return $count; } public function getByForeachAfterForeach() { $directory = new RecursiveDirectoryIterator($this->path); $count = 0; foreach ($directory as $filename => $fileInfo) { // do nothing } foreach ($directory as $filename => $fileInfo) { $count++; } return $count; } public function getByLoop() { $directory = new RecursiveDirectoryIterator($this->path); $count = 0; while ($directory->valid()) { $count++; $directory->next(); } return $count; } public function getByIteratorToArray() { $directory = new RecursiveDirectoryIterator($this->path); return count(iterator_to_array($directory)); } public function getByIteratorToArrayWithRewind() { $directory = new RecursiveDirectoryIterator($this->path); $directory->rewind(); return count(iterator_to_array($directory)); } } $test = new Test(); $test->createTestFiles(); $test->runTest(); # comment so that 3v4l sees a change in the file
Output for git.master_jit, git.master, rfc.property-hooks
get by foreach: 65 get by foreach with rewind: 65 get by foreach after foreach: 65 get by loop: 65 get by iterator_to_array: 65 get by iterator_to_array with rewind: 65

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:
79.08 ms | 405 KiB | 5 Q