note 39091 added to language.variables.scope

From: Date: Fri, 16 Jan 2004 20:34:56 +0000
Subject: note 39091 added to language.variables.scope
Groups: php.notes 
Request: Send a blank email to php-notes+get-63698@lists.php.net to get a copy of this message
Whats good for the goose is not always good for the iterative gander. If you declare and initialize the static variable more than once inside a function ie. function Test(){ static $count = 0; static $count = 1; static $count = 2; echo $count; } the variable will take the value of the last declaration. In this case $count=2. But! however when you make that function recursive ie. function Test(){ static $count = 0; static $count = 1; static $count = 2; $count++; echo $count; if ($count<10){ Test(); } } Every call to the function Test() is a differenct SCOPE and therefore the static declarations and initializations are NOT executed again. So what Im trying to say is that its OK to declare and initialize a static variable multiple times if you are in one function... but its NOT OK to declare and initialize a static variable multiple times if you call that same function multiple times. In other words the static variable is set once you LEAVE a function (even if you go back into that very same function). ---- Manual Page -- http://www.php.net/manual/en/language.variables.scope.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+39091 Delete -- http://master.php.net/manage/user-notes.php?action=delete+39091&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+39091&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#63698) next »