so running issue figured out how fix, curious why. here block of code
<?php function test($attempt=1){ if ($attempt == 5){ echo "done!"; }else{ echo 'recursion!'; test($attempt++); } } $test = test();
now code should run first time, check, go else statement run test again time $attempt++ until == 5 , echo done , complete. not work , loops forever. can fixed assigning variable variable after entering function
<?php function test($attempt=1){ $nextattempt = $attempt+1; if ($attempt == 5){ echo "done!"; }else{ echo 'recursion!'; test($nextattempt); } } $test = test();
any ideas why is?
you want pre-increment instead of post-increment of variable. increment variable $attempt
before passing argument function instead of after.
so want test(++$attempt);
instead of test($attempt++);
.
sandbox working example: http://sandbox.onlinephpfunctions.com/code/c50731815588d55bd079e701f1c5dde9e7148696