131

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
  • 43,201
  • 71
  • 221
  • 382
barbushin
  • 5,165
  • 5
  • 37
  • 43
  • 13
    100 seems a little excessive, even for an event framework. – Ignacio Vazquez-Abrams Nov 27 '10 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. – Abel Dec 28 '11 at 12:50
  • http://stackoverflow.com/a/36440027/2652524 I solve mine using this answer – Gujarat Santana Apr 06 '16 at 01: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. – lilHar Apr 14 '21 at 21:40

3 Answers3

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
  • 13,207
  • 5
  • 76
  • 94
netcoder
  • 66,435
  • 19
  • 125
  • 142
  • 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. – SkaJess Oct 28 '14 at 13:39
  • 9
    If you don't have this `xdebug.max_nesting_level = 100` option in your ***php.ini*** just paste that in. – M. Reza Nasirloo Mar 16 '15 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 – Enrique Quero Feb 17 '16 at 16:45
  • 3
    @EnriqueQuero Depends on the system and OS. – netcoder Feb 17 '16 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); – user2928048 May 09 '16 at 14:19
  • On mac osx the location of the file for me was usr/local/php5/php.d/50-extension-xdebug.ini – Sarthak Gupta May 24 '16 at 17:27
  • Since Xdebug 2.3 the default value is `256`. – martin Mar 13 '17 at 13:40
  • good shot ini_set('xdebug.max_nesting_level', 300); works for me – Hassan Saeed Jul 16 '17 at 14:49
13

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.

Xnuiem
  • 153
  • 6
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.

Antony
  • 3,875
  • 30
  • 32