Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP File Handling with PHP Basic File Handling Directory and File Structures

Logic question

I am watching the video of the course. Somehow, i do not understand the logic of calling own function on its own scope. Like in video, the function already created with readopenfolder(), but within the readopenfolder() function we call this function again. I don't know the logic behind this line. Please tell me the logic of this line. Thank you very much.

1 Answer

This is a classic example of what is called "recursion", which is calling the same function within itself. The concept was actually alluded to in the movie "Inception" from 2010 starring Leonardo DiCaprio. And the Treehouse video is actually a great example of how it used and why it is necessary.

I once had to create a function (let's say it's called deleteFolder() ) that would delete all files and folders in a folder in a file system that would not allow you to delete a folder unless it was empty (void of files).

The function I came up with took a folder as a parameter.

In the function, the code would determine if each item in that folder was a file or a folder.

If it was a file, it would delete it...

if not (this is where the recursion occurs)

the function would have to call deleteFolder() inside itself to process the new/nested folder.

the last thing the function would do is delete the folder once it's empty and exit back out to the parent function and resume processing the outer level's list of files (and/or folders).

Rinse. Repeat. Etc. (Until the whole nested file structure is deleted and you've exited all the way out of the top-level deleteFolder() functionl call).

Does that make sense?

Here is another simple, but great, and common example of recursion usage:

https://www.w3resource.com/javascript-exercises/javascript-recursion-function-exercise-1.php

By the way, I was blown away when I found out that recursion is even possible. I'm amazed that it works!?! It's kinda like somehow inserting Deja Vu into your code. LOL

One caveat, though, is that depending on how deep your recursion goes, it can be fairly resource-expensive (the deeper the function nesting, the more memory/stack is used). See this: https://www.quora.com/Is-recursion-appropriate-for-optimal-CPU-usage-and-what-are-the-alternatives

Anyway, I hope that all helps.

Stay safe and happy coding!

Thank you Peter!! Your explaination is very helpful. Thank you very much.