Looking for infinite loops and recursions in PHP code From time to time you get some very nasty errors like infinite recursion or infinite loop. Being able to locate the error is crucial to quickly solving the problem.
Here are two tips that can help you find such nasty bugs in PHP code.
Warning - executing code from this article may be harmful to your system!
Run this code only in testing environment (never in production).
PHP script uses up all CPU and does not want to end
To simulate infinite loop create file like this
function a(){
$z=0;
while(1){
$z++;
$z = $z/2;
}
}
function b(){
a();
}
function c(){
b();
}
c();
and now try to find the location. Easiest way to locate source of infinite loop is to add a time limit on all scripts. To do that set max_execution_time setting to a very low number like 2-5 seconds and rerunning the script. You should see message like:
Fatal error: Maximum execution time of 10 seconds exceeded in /path/test.php on line XXX
That should be enough to let you find the code that is causing the problem. Sometimes the infinite loop is somewhere way up in the execution stack. Then every time script times out it will report different part of the code. You cant find which loop is faulty as too much code is covered by it.
If you cant see which loop runs forever set a breakpoint in any of the places reported by time limit and run in debugger. Then you will easily see where is the broken loop as you can go up the execution stack and inspect the context of each level.
PHP script crashed with apache stack dump
Fatal errors like that in PHP core or PHP extensions are extremely rare. So far in my life it happened to only twice or three times that there was a PHP bug crashing my apache. All the other times it was our PHP application code causing the problem.
Whenever you see segmentation fault in your apache error_log its most of the time infinite recursion.
Fatal error like that will often seem as blank page and null output. If you have some content sent using flush or headers sent to client you will only get that and content will break.
In logs (apache error log) it will look like this:
child pid 79774 exit signal Segmentation fault (11)
You can easily see the place where infinite recursion occurred by enabling xdebug. Set xdebug to activate by default. Usually error with stack trace should be enough to locate the broken code or set debugger breakpoint.
Here are necessary settings for xdebug to make your life easier:
zend_extension= /path/xdebug.so xdebug.default_enable = On xdebug.profiler_enable = 1 xdebug.profiler_output_dir = /tmp xdebug.trace_format = 0
This should output a nice stack trace on your page with a message like this:
Fatal error: Maximum function nesting level of '100' reached, aborting! in /path/test.php on line 11
Code to test that would look something like this:
function a(){
$x=1;
$y=1;
$z=1;
return a();
}
function b(){
a();
}
function c(){
b();
}
c();
Summary of PHP crashes
Its worth to remember that you should control max memory limit, max execution time and max stack depth of all your scripts. Its also good idea to have system level limits in case of apache/php error.
Another important thing! Max execution time only counts the actual time spent in the PHP code! it does not count IO. So if you have slow SQL queries your script may actually run for much much longer time. Also disk operations and network read/write will not be included in this time limit.
Hope it helps
Main Blog Categories
About the author

Hi, my name is Artur Ejsmont,
welcome to my blog.
I am a passionate software engineer living in Sydney and working for Yahoo! Drop me a line or leave a comment.
Enjoy!
Comments
Thanks for the helpful
Thanks for the helpful post.
KimkasJK
Post new comment