Reputation: 198204
I know they are, the long version of the question is:
Where in the PHP manual is it stated that static function variables are initialized to NULL and considered empty?
<?php
function foo() {
static $static;
}
Example: Inside foo, to which value is $static initialized on first call?
I remember that this was written in the PHP Manual but I did not bookmark the location. Over the last three days I tried to find it again but no luck.
I'm looking for a document with authority, so the statement can be stressed for. For example a page in the PHP manual that is making clear which values those initialized to if none specified, unfortunately, Using static variables in the manual is not giving any information about this. A proof based on on the source-code of the PHP language (not a PHP script) would be sufficient as well, I'm still not good at reading the PHP sourcecode.
Upvotes: 3
Views: 1280
Reputation: 48304
http://us.php.net/manual/en/language.types.null.php:
A variable is considered to be null if:
it has been assigned the constant NULL.
it has not been set to any value yet.
it has been unset().
Since a static variable is a variable, then the above (second point) sufficiently answers your question. The burden of proof is now on someone to show that the manual states that static variables behave differently.
I think one will be disappointed if he expects the manual to have a special section for every type of variable explaining in duplicate fashion how each work exactly the same way regarding initialization, casting, mathematical operations, etc.
Upvotes: 4
Reputation: 468
Go to http://www.php.net/manual/en/language.variables.basics.php and scroll down to Example #1 Default values of uninitialized variables. You will see it explains
Unset AND unreferenced (no use context) variable; outputs NULL
Upvotes: 2
Reputation: 5313
Static variables are initialized to null.
class MyClass {
static $var;
}
var_dump(MyClass::$var); // returns NULL
Upvotes: 0
Reputation: 51411
I'm looking for a document with authority
You don't need no steenking document. Try testing the code. From PHP's interactive shell:
php > function foo() { static $bar; var_export($bar); }
php > foo();
NULL
Thus we know that at least as of PHP 5.3, static variables work just like normal variables: they are null
until given a value.
What did you expect? Why are you asking this question? Are you getting weird behavior?
Upvotes: 1
Reputation: 360782
Given that there's really one one implementation of PHP out there, you could take any proof-of-concept script and take its output as the "definitive" proof:
marc@panic:~$ cat z.php
<?php
function foo() {
static $bar;
var_dump($bar);
}
foo();
marc@panic:~$ php z.php
NULL
so, yes, it would appear that declared-but-not-explicitly-initialized static vars are assigned the NULL value.
Upvotes: 0