can a function call itself in php? [SOLVED *why yes, it can*

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
w35z0r
Forum Newbie
Posts: 17
Joined: Thu May 18, 2006 7:02 pm

can a function call itself in php? [SOLVED *why yes, it can*

Post by w35z0r »

Hello,
I've run into an interesting problem. I wrote this little function to basically take a comma separated string (ex: value, value, another value, value) and make sure theres not an extra comma on the tail or the front (ex: value, value,)

Here is the function:

Code: Select all

function csvTrim($string){      
        $csv = trim($string);
        $numOfCommas = substr_count($csv, ",");
        $csvArray = explode(",", $csv);
        foreach($csvArray as $value) {
            if($value != ""){
                $numOfValues++;
            }
        }
        if($numOfCommas-1 >= $numOfValues) {
            //more commas than values?! time to trim
            if($csv[0] == ",") {
                $trimedCsv = substr_replace($csv, '', 0,1);  //deletes first character
                $returnCsv = csvTrim($trumedCsv); 
                return $returnCsv;
            }else{
                $trimedCsv = substr_replace($csv, '', -1);  //deletes last character
                $returnCsv = csvTrim($trumedCsv); 
                return $returnCsv;
            }
        }else{
            return $csv;
        }
    }
Note the function calls itself. Any page that calls this function does not even load. I get Unable to connect error from firefox, like the page isn't even there (which it is). I take the function out of the page, boom: it loads, no problem.

I'm running an apache server as installed by Xampp 1.6.6a

Also, if you can think of a nicer way to achieve what I'm after with the function, that'd be appreciated too! =]
Last edited by w35z0r on Tue Jul 22, 2008 1:09 pm, edited 1 time in total.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: can a function call itself in php?

Post by Eran »

A function can certainly call itself resulting in recursion which is a powerful technique. However when using it you should beware of infinite recursion - make sure there is a stopping condition and that it will be met otherwise the recursion will never end causing your script to either timeout or throw an infinite recursion error (depending of the speed of each iteration).
User avatar
w35z0r
Forum Newbie
Posts: 17
Joined: Thu May 18, 2006 7:02 pm

Re: can a function call itself in php?

Post by w35z0r »

Excellent, I was hoping that would be the case. I assume that the return statement counts as a stopping condition? If so, then I must have over seen something. I'll comb my code a little more and mark the thread solved if this is the case.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: can a function call itself in php?

Post by Eran »

In your case, the stopping condition is the one that decides whether to call the function again:

Code: Select all

 
  if($numOfCommas-1 >= $numOfValues) {
 
My guess is that the condition is never satisfied. Maybe add a counter to limit the number of possible iterations, and make sure it never exceeds that number.
User avatar
w35z0r
Forum Newbie
Posts: 17
Joined: Thu May 18, 2006 7:02 pm

Re: can a function call itself in php?

Post by w35z0r »

Thats a good idea, I'll be sure to put it in there.

I found what the problem was: fat fingers. My recursion line,

Code: Select all

$returnCsv = csvTrim($trumedCsv);
calls the function on an undefined variable, $trumedCsv, its supposed to be $trimedCsv (actually, probably trimmedCsv for spellings sake).

After that, the function at least worked. doesn't quite do what I want, but I can work that out later.

Sigh.
Post Reply