is there a way to loop a loop in php?
if i got a function for example, could i loop this function endless times?
in php6 there is goto. but how could u do this in php5?
function getLinks($link) {
// step 1: if $link got links, add them in a array
// step 2: iterate array of links
// step 3: save current link
// step 4: check if current link got links, if it has, run $this->getLinks($link)
}
in step 4 i need to use the function getLinks. And as you can see i want to make a crawler that crawls every link and saves them.
has someone done anything similar, tried to loop/crawl all levels of links?
i think i need the Goto function here but is there a way to accomplish this in php 5?
i have no idea how i could do this...thanks in advance!
Yes, this is possible. If you are calling the same method inside of the method, you are using recursion. One of the big things to remember about recursive loops is to ensure you add a base case to stop the loop, otherwise you'll loop continuously and that's not good.
I would suggest not going the recursive route, its a lot of overhead and should be your last resort. Using a while loop should achieve similar results.
Its called a recursive function (just call the function again in the body of the function). However, if you're building a crawler, I'd caution you to be well behaved. You can check out http://www.robotstxt.org/. Things like the amount of time between requests on the same domain can be limited by the owners robots.txt file. If you don't behave well, you can get banned by many sys admins.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With