MDanz Posted February 26, 2011 Share Posted February 26, 2011 function width2($noid) { $getwidth = mysql_query("SELECT * FROM block WHERE sid='$theid'",$this->connect); while($row1 = mysql_fetch_assoc($getwidth)) { $this->width2($noid); } } how do i count how many times the function loops? Quote Link to comment https://forums.phpfreaks.com/topic/228907-count-in-function-recursion/ Share on other sites More sharing options...
.josh Posted February 26, 2011 Share Posted February 26, 2011 You can use a static variable. Static variables retain their values each time you call a function. It's sort of like a global variable but within the function's scope. (You can of course also just use a global variable, but this keeps it with the function scope). Normal: function blah () { $x++; echo $x; } for ($y = 0;$y<10;$y++) { blah(); } // output: 1111111111 Static function blah () { static $x; $x++; echo $x; } for ($y = 0;$y<10;$y++) { blah(); } // output: 12345678910 Quote Link to comment https://forums.phpfreaks.com/topic/228907-count-in-function-recursion/#findComment-1180020 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.