Alok Rai Alok Rai - 1 year ago 55
PHP Question

Recursion function's variable value not changing in PHP

I want to print 1 to 10 numbers using recursion but its not working.


function table($num1) {
if( $num1 <= 10 ) {
echo $num1;
echo "<br>";


$num = 1;

Here's the error

Fatal error: Maximum function nesting level of '256' reached,

But when I am declaring
as global its working fine. Please anyone tell me the reason behind it.

Answer Source

table($num1++) means please pass $num1 to table(), and then increase it by one. So this is not what you want.

You have to write table(++$num1) instead. It means increase $num1 first, then pass it to table().