Isn't recursion the one with depth-first and with the stack we have a breadth-first search? With the stack solution shown we are reading all elements in the current directory first and pushing new directories to the stack and then starting a new loop iteration with the last directory added to the stack. With recursion one "goes into" another recursion step as long as a directory is encountered.
You can have stack behavior with arrays (shift/unshift elements) but it arrays in PHP consume a lot of memory (because they are actual dictionaries and not "classical arrays" in other languages).
Isn't recursion the one with depth-first and with the stack we have a breadth-first search? With the stack solution shown we are reading all elements in the current directory first and pushing new directories to the stack and then starting a new loop iteration with the last directory added to the stack. With recursion one "goes into" another recursion step as long as a directory is encountered.
Wow. What is the benefit of using a stack object of just simply using an array? It seems that the stack object would be way slower.
You can have stack behavior with arrays (shift/unshift elements) but it arrays in PHP consume a lot of memory (because they are actual dictionaries and not "classical arrays" in other languages).