Page 1 of 1

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

Posted: Tue Jul 22, 2008 12:33 pm
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! =]

Re: can a function call itself in php?

Posted: Tue Jul 22, 2008 12:36 pm
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).

Re: can a function call itself in php?

Posted: Tue Jul 22, 2008 12:43 pm
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.

Re: can a function call itself in php?

Posted: Tue Jul 22, 2008 12:46 pm
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.

Re: can a function call itself in php?

Posted: Tue Jul 22, 2008 1:08 pm
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.