Re: Static and Global at the same time!
| From: | Jochem Maas | Date: | Fri, 10 Feb 2006 15:25:52 +0000 |
| Subject: | Re: Static and Global at the same time! | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-230212@lists.php.net to get a copy of this message | ||
Miguel Guirao wrote:
Hello list!!my name is not 'list' but I'll let you off this time.
Can I have a variable that is static and global at the same time within a function?here is some [linux] command line output that took about 30 seconds to generate, it answers your question. you'll have to work out the answer yourself by reading the output - next time why not run the test code yourself (unlike most people you actually wrote some code to test) ??? $> php -r ' $data = ""; function newdata() { static $data; global $data; if (empty($data)) { $data = "TEST"; } return $data; } var_dump($data, newdata(), $data, newdata()); ' string(0) "" string(4) "TEST" string(4) "TEST" string(4) "TEST" $> php -r ' $data = ""; function newdata() { global $data; static $data; if (empty($data)) { $data = "TEST"; } return $data; } var_dump($data, newdata(), $data, newdata()); ' string(0) "" string(4) "TEST" string(0) "" string(4) "TEST" $> php -r ' $data = ""; function newdata() { static global $data; if (!isset($data)) { $data = "TEST"; } return $data; } var_dump($data, newdata(), $data, newdata()); ' Parse error: parse error, unexpected T_GLOBAL, expecting T_VARIABLE in Command line code on line 3
Let's say I have: $data = ""; function newdata() { static global $data; $data [$i] = $newdatatobestored_inthearray; } Best Regards, ----------------------- Miguel Guirao Aguilera Logistica R8 TELCEL Tel. (999) 960.7994