Static variable in PHP -


if run following php code, 123. don't understand logic behind it. think when call function each time suppose output 1. output should 111.

function keep_track() {   static $count = 0;   $count++;   print $count; }  keep_track(); keep_track(); keep_track();  // output 123 

i know static variable holds value after function exits in above function assigning value in first line, yet still adding +1 previous value of $count.

could please explain this? (i sorry if sound stupid.. trying find out how happening)

$count initialized in first call of function , every time method called, increments $count.

in link, scroll down using static variables better understanding.


Comments

Popular posts from this blog

php - cannot display multiple markers in google maps v3 from traceroute result -

php - Boolean search on database with 5 million rows, very slow -

css - Text drops down with smaller window -