How to debug, and protect against, infinite loops in PHP?

12,079

Solution 1

You could use a debugger such as xdebug, and walk through your code that you suspect contains the infinite loop.

Xdebug - Debugger and Profiler Tool for PHP

You can also set

max_execution_time

to limit the time the infinite loop will burn before crashing.

Solution 2

I sometimes find the safest method is to incorporate a limit check in the loop. The type of loop construct doesn't matter. In this example I chose a 'while' statement:

$max_loop_iterations = 10000;
$i=0;
$test=true;
while ($test) {

  if ($i++ == $max_loop_iterations) {
    echo "too many iterations...";
    break;
  }

  ...
}

A reasonable value for $max_loop_iterations might be based upon:

  • a constant value set at runtime
  • a computed value based upon the size of an input
  • or perhaps a computed value based upon relative runtime speed

Hope this helps, - N

Solution 3

Unit tests might be a good idea, too. You might want to try PHPUnit.

Share:
12,079
Robert K
Author by

Robert K

I am a web designer & developer versed in HTML5, and CSS. I program web applications in PHP 7. My toolset typically consists of PHP Storm, Sublime Text 3, VS Code, Sequel Pro, Transmit, and Photoshop.

Updated on June 16, 2022

Comments

  • Robert K
    Robert K almost 2 years

    I recently ran up against a problem that challenged my programming abilities, and it was a very accidental infinite loop. I had rewritten some code to dry it up and changed a function that was being repeatedly called by the exact methods it called; an elementary issue, certainly. Apache decided to solve the problem by crashing, and the log noted nothing but "spawned child process". The problem was that I never actually finished debugging the issue that day, it cropped up in the afternoon, and had to solve it today.

    In the end, my solution was simple: log the logic manually and see what happened. The problem was immediately apparent when I had a log file consisting of two unique lines, followed by two lines that were repeated some two hundred times apiece.

    What are some ways to protect ourselves against infinite loops? And, when that fails (and it will), what's the fastest way to track it down? Is it indeed the log file that is most effective, or something else?

    Your answer could be language agnostic if it were a best practice, but I'd rather stick with PHP specific techniques and code.

  • Robert K
    Robert K almost 15 years
    This was in a 100 line codebase; not much there to hang me up on. The trouble was rewriting one routine of an intertwined pair of routines, and reversing the call flow. a->b->c became a<->b.
  • Robert K
    Robert K almost 15 years
    I'd completely forgotten about xdebug. Thanks!