132

There is one very bad limit in PHP: if you call some function a1() that calls a2(), that calls a3... so when a99() will call a100() you will see

Fatal error: Maximum function nesting level of '100' reached, aborting!

Is there any way to increase the limit of 100 nesting calls to 500 or 10000?

This is critical for me because I'm developing an event-based system with a lot of callbacks.

Mridang Agarwalla
45.3k74 gold badges238 silver badges397 bronze badges
asked Nov 27, 2010 at 20:34
4
  • 13
    100 seems a little excessive, even for an event framework. Commented Nov 27, 2010 at 20:47
  • 7
    @Ignacio: it's quite simple, even if the focus is on an event framework, to have a recursive function that needs a much higher nesting level. Tens of thousands (or even millions) is not uncommon in such scenario's. Commented Dec 28, 2011 at 12:50
  • stackoverflow.com/a/36440027/2652524 I solve mine using this answer Commented Apr 6, 2016 at 1:47
  • @IgnacioVazquez-Abrams If you're doing functional programming instead of object-oriented or procedural, 100 easily is a drop in the bucket. And considering many PHDs in the field recommend using functional over OOP or proc, really, you should always be prepared to have lots of recursive functions. Commented Apr 14, 2021 at 21:40

3 Answers 3

268

This error message comes specifically from the XDebug extension. PHP itself does not have a function nesting limit. Change the setting in your php.ini:

xdebug.max_nesting_level = 200

or in your PHP code:

ini_set('xdebug.max_nesting_level', 200);

As for if you really need to change it (i.e.: if there's a alternative solution to a recursive function), I can't tell without the code.

Potherca
14.8k5 gold badges86 silver badges108 bronze badges
answered Nov 27, 2010 at 20:53
8
  • I had the same problem. My max_nesting_level was set to 100 but in my case, in some cases, it's possible to have 1000 recursive calls. So, I set to 10000 to avoid this xdebug error. In all cases, it always better than let PHP with no limit. Commented Oct 28, 2014 at 13:39
  • 9
    If you don't have this xdebug.max_nesting_level = 100 option in your php.ini just paste that in. Commented Mar 16, 2015 at 10:24
  • @Pedram The correct way to copy that in a php ini file is in /etc/php5/apache2/conf.d/20-xdebug.ini, not in the normal php.ini. Just a good practice Commented Feb 17, 2016 at 16:45
  • 3
    @EnriqueQuero Depends on the system and OS. Commented Feb 17, 2016 at 17:04
  • It works! No matter if you use XDebug or not, neither if you comment out line in php.ini. I explicitly used: ini_set('xdebug.max_nesting_level', -1); Commented May 9, 2016 at 14:19
14

Do you have Zend, IonCube, or xDebug installed? If so, that is probably where you are getting this error from.

I ran into this a few years ago, and it ended up being Zend putting that limit there, not PHP. Of course removing it will let you go past the 100 iterations, but you will eventually hit the memory limits.

answered Nov 27, 2010 at 20:50
1

Personally I would suggest this is an error as opposed to a setting that needs adjusting. In my code it was because I had a class that had the same name as a library within one of my controllers and it seemed to trip it up.

Output errors and see where this is being triggered.

answered May 10, 2017 at 11:32

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.