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? 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 Link to comment https://forums.phpfreaks.com/topic/228907-count-in-function-recursion/#findComment-1180020 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.