$count = 5;
function get_count()
{
static $count = 0;
return $count++;
}
echo $count;
++$count;
echo get_count();
echo get_count();
I guessed it outputs 5 0 1 and it's right,but I need a better explanation?
$count = 5;
function get_count()
{
static $count = 0;
return $count++;
}
echo $count;
++$count;
echo get_count();
echo get_count();
I guessed it outputs 5 0 1 and it's right,but I need a better explanation?
First echo: Gives you the variable $count that you declare in your first line.
Second echo: calles get_count which creates the static variable $count (so it's in context for this function) and when you instantiate the static variable you're setting it to zero. return $count++ is one of those lines we usually avoid in code - but essentially, it is incremented AFTER the value is returned.
Third echo: Likewise, 0 was incremented to 1 after the previous call to get_count, the same happens here - it returns 1 and increments the value to 2.
Does that help or is that actually more confusing?
Well, first of all, $count inside the function and $count outside of the function are 2 different variables. That explains why the first output prints 5.
Now for the static: Static means your local variable is created only once, when the function executes for the first time. Every function execution afterwards uses the same variable, so the latest values of the variable in the last function execution is still there.
So, when you first call get_count(), the variable is set to 0 and then returned. After the return, the variable is incremented.
When you call the function for the second time, the variable is still 1. This value is returned and then incremented to 2.
The variable $count
in the function is not related in any kind to the global $count
variable. The static
keyword is the same as in C or Java, it means: Initialize this variable only once and keep its state when the function ends. This means, when execution re-enters the function, it sees that the inner $count has already been initialized and stored the last time as 1
, and uses that value.
You have two separate variables that are both called $count, but they have a different scope. The first variable is not explicitly declared, but comes into existence when you first assign it.
The second variable (inside the method) is only visible to that method. Since it's static, its value is retained between multiple executions of the same method. The assignment $count = 0;
is only executed the first time the method is run.
As for the increment operator (++), the result of the evaluation is the value before it was incremented, because the (unary) operator comes after the variable name. So yes, the output would be 5, 0, 1.
If you were to write return ++$count;
, the result would have been 5, 1, 2.
Note: the ++$count
you have in your existing code, it is effectively equivalent to $count++
, since the result of the evaluation is discarded. The effect to the $count
variable is the same: it gets incremented by 1.
$count = 5; // "outer" count = 5
function get_count()
{
static $count = 0; // "inner" count = 0 only the first run
return $count++; // "inner" count + 1
}
echo $count; // "outer" count is still 5
++$count; // "outer" count is now 6 (but you never echoed it)
echo get_count(); // "inner" count is now + 1 = 1 (0 before the echo)
echo get_count(); // "inner" count is now + 1 = 2 (1 before the echo)
echo get_count(); // "inner" count is now + 1 = 3 (2 before the echo)
I hope this clears your mind.