Recursion in PHP

June 10, 2007

Recursion is the mechanism in which a function calls itself. This is a powerful feature which can make something complex something simple.

And PHP manual clearly mentions that….

“It is possible to call recursive functions in PHP. However avoid recursive function/method calls with over 100-200 recursion levels as it can smash the stack and cause a termination of the current script”

 Recent PHP Security. Org article ( PHP Executor Deep Recursion Stack Overflow) tells you that, all PHP versions are still affected and server crashes and that will kill all other threads of a multithreaded webserver.

 The problem is in own words of Zeev “PHP 4.0 (Zend) uses the stack for intensive data, rather than using the heap” and stack fills up pretty quickly in deep recursion.

One way to fix (not the recursive problem but crashing the server) is to use an extension like XDebug to specify the maximum recursion depth thatway php exits with an error but wont crash.

Any more ideas?

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: