I have this problem,

When I run the script, which contains a recursive function that has a global variable ..

I get this error:

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, admin@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

I used to get this error only when the file I try to run does not exist.. but it's happening in this function ..

The apache error log says:

[Thu Feb 07 16:15:02 2002] [error] Cannot remove module mod_speling.c: not found in module list

The function is:

function topics($topicid,$i)
{
	global $topics; //This is the global var, an array

            echo ($topics[$i]['cat_name']);

if ($i>=count($topics))
{
	return "";
}
else
{
                            $i++;
	topics($topics[$i]['id'],$i);
}
}

I hope this is not a bug in php..

Can someone help me .. I really need to use recursion ..

    Oh sorry there is an error in the error log.. I have written an error happened in 2002 😃
    It's just because I picked up the first line .. too lazy to get to the last one 😃

    Anyway, the apache error log says:

    [Fri Dec 26 08:34:26 2003] [error] [client 127.0.0.1] Premature end of script headers: c:/apache/php/php.exe

      If you recurse too deep, the stack will overflow and the process will terminate. That might give the error you see. How big is your $topics array?

      Also, why are you using recursion in that function? The process is clearly an iterative one, ideally suited to a simple for loop.

        Nah, unless you've got a lot of topics, there probably isn't that much
        recursion going on.

        I too am at a total loss as to why one would want to use recursion for such a
        simple loop.

        function topics($topicid,$i)
        {
            global $topics; //This is the global var, an array
        	$count = count($topics);
        	for($t=$i; $t<count($count); $t++)
        	{	echo $topics[$t]['cat_name'];
        	}
        	return "";
        }
        

          Thanks for the great input

          I think it might be a stack overflow, since the topics array has about 1000 entry nested as categories and sub-categories..

          This nesting forced me to think of using recursion to combact the code .. because I need to show the topics herarichally..

          I tried to ( simulate ) the recursion process using stacks .. and thankfully I succeeded ..

          I just want to say something I just found out,

          IT IS VERY BAD TO ASSIGN GLOBAL VARIABLES IN FUNCTIONS FOR LARGE ARRAYS.

          Yes it is really bad..

          Try to make it local as much as possible..

          When I put the array as global variable: The script ran in almost 4 sec
          But as a local variable it never exceeded two seconds..

          I believe this also justifies the reason why XML parsing is very slow for ( LARGE ) xml files in php .. it will definitely be very slow if you were using global array variables in the functions (opening tag, closing tag, cdata handler) .. I can't think of a solution for the XML thing right now .. but the original problem of this thread was resolved..

          Thanks.

            Originally posted by Lahloob
            I think it might be a stack overflow, since the topics array has about 1000 entry nested as categories and sub-categories..

            This nesting forced me to think of using recursion to combact the code .. because I need to show the topics herarichally..

            Recursion probably does make sense in that instance, but properly coded it should not recurse more levels than levels of nesting. So if your heirarchy can be up to 5 levels deep, there should not be more than 5 levels of recursion. The example you posted recursed one level for every item, which would mean 1000 levels if the array has 1000 entries. That can easily blow the stack.

            If you're sorta new to recursion, learn about and implement merge sort. It's a nice elegant example of a recursive algorithm (simpler than quicksort, IMHO). To sort a thousand items it only needs to recurse 10 levels deep; to sort a million items it only recurses 20 levels deep.

            Some languages support "tail recursion" which would allow the example you posted to not blow the stack, but most (including PHP) do not.

              You really only need to be recursing each time you go down into a subcategory, and popping back up afterwards. You will blow the stack if your hierarchy really is 1000 levels deep, but not if it's only half a dozen (the size of the stack is supposed to be dependent on depth, not the number of entries).

              If it is a hassle, you could store the hierarchy structure yourself by structuring your data suitably as you read it in.

                Write a Reply...