I have such a function, and I declare the same static variable twice with different values. Then, I called the function, but the result surprised me.
function question(){
static $a=1;
$a++;
echo $a; // output:?
static $a=10;
$a++;
echo $a; // output:?
}
I thought the outputs would be: 2 11, but the outputs was: 11 12. Why?
If you declare and initialize the same static variable more than once inside a function, then the variable will take the value of the last declaration (static declarations are resolved in compile-time.)
In this case, the static variable of $a will take the value of 10 in the compile time, ignoring the value of 1 in the previous same declaration.
Static works the same way as it does in a class.
The variable is shared across all instances of a function.
so if you initialize same static variable many time then it will always take latest value.
A static variable exists only in the declared local function scope, but it does not lose its value when program execution leaves this scope.
The use of Static keyword is itself such that it will not lose track of the current count.
So In your case, function execution stops at $a = 10; $a++;
Hence you have 11 and 12 as output.
If you want output to be 2 and 11; keep only one declaration static like below.
function question(){
$a=1;
$a++;
echo $a; // output:?
static $a=10;
$a++;
echo $a; // output:?
}
Related
I saw this on W3Schools.
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>
The output is 0, 1 and 2.
I wonder why it is not 0, 0 and 0.
Since each time the function is called, the variable x becomes 0 again.
I am a PHP beginner. Thanks!
If you declare a var static inside a function, the var keep it value over multiple calls. You could compare it to a static var inside of classes.
The code you post is a good example to see the actual effect. However I would only carefull use static inside functions, because most of the time, you need the static value somewhere else, reset the value, or something else which requires to much logic and makes the code really bad.
A good example would be a function, which returns a unique identifier for a given identifier. This could be simply achieved by using this code.
function unique_id($id) {
static $count = 0;
return $id . ($id++);
}
This example may clarify. static has a scope, thus is not a global variable. So I can define static $x outside the function and it will be defined there. Since static has scope, it wouldn't make any sense to keep on executing and resetting $x = 0. So, php will only acknowledge it the first time that line is called.
<?php
static $x = 1000;
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>
What's the difference between function &foo() and function foo(&$var) in PHP?
Example in code:
<?php
function foo(&$var){
$var++;
}
function &bar(){
$a= 5;
return $a;
}
foo( bar() );
The main issue here is who wants to alter or read whose variable.
In the first example you want the outer variable to be changed by the function. In example two you want the outer world to change the inner variable. And you can get changed values from the different scope.
A better use case for the second version would be:
class example {
public $test = 23;
public function &exposeTest() {
return $this->test;
}
}
$example1 = new example;
$testref = &$example1->exposeTest();
$testref++;
echo($example1->test); // 24
$example1->test++;
echo($testref); // 25
So it is not really a difference besides design issues and without OOP may not matter any way.
I have a question in my todays Exam in which I have to determine the output.
<?php
function statfun($x)
{
static $count=0;
$count += $x;
if ($count < 20) {
echo "$count <br>";
statfun(++$x);
} else {
echo "last num is $count";
}
}
statfun(2);
?>
The output is
2
5
9
14
last num is 20
I dont know why this is the output. I know it is due to the static member but each time it comes into the function the member $count is re-initialized.I had saw the documentation at Static Keyword.
But there is nothing written regarding the re-initialization of static variable? Can we re-initialize the static variable in PHP? With the same or any other value?
each time it comes into the function the member $count is re-initialized
This is incorrect. Static variables are initialized only once which is how statically declared variables differ from "ordinary" variables. So basically, you're assigning an initial value to $count. In multiple calls to statfun(), this static variable's value is preserved and can be reused.
From the manual, section "Using static variables":
A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.
Also look at the example-code in the manual. The difference stated there should answer your question.
when you pass 2 count is set to 2
with $count+=$x;
then you have called statfun(++$x) which is $x+1 and that is 2+1=3
so now $count will be $count+3 and that is 5, and then you call statfun with the value of 3 then $count will $count+(3+1) = 9 and so on and so on
static variable will hold its state. So if you call it like this
So basically static variable will hold its value and will not be re-initialized.
Recently i was studying the "Passing by Reference", I come to know following ways
What is the main difference between the following methods.
1.
function foo(&$var)
{
$var++;
}
$a=5;
foo($a);
2.
function foo($var)
{
$var++;
}
$a=5;
foo(&$a);
3.
function foo(&$var)
{
$var++;
}
function &bar()
{
$a = 5;
return $a;
}
foo(bar());
even though all of them produce same results, and which is the best way to work with.
Thanks.
function foo(&$var)
{
$var++;
}
$a=5;
foo($a);
This accepts a parameter that is always passed by reference (the & is in foo(&$var)). When $a is passed, it's always as a reference, so incrementing the variable inside the function will cause the parameter to be modified.
function foo($var)
{
$var++;
}
$a=5;
foo(&$a);
Do not use this. This is call-time pass-by-reference (you're passing &$a, a reference to $a, into the function), and is deprecated as of PHP 5.3.0. It's bad practice because the function doesn't expect a reference.
function foo(&$var)
{
$var++;
}
function &bar()
{
$a = 5;
return $a;
}
foo(bar());
This returns a reference (the & is in &bar()) to a variable $a declared in the function bar(). It then takes a reference to the return value of bar() and increments it. I'm not sure at a glance why this would be useful, though, especially for primitive/scalar types.
The second method is deprecated and should never be used.
Typically the function should just return the value.
function foo($a)
{
$a = 5;
}
$a = foo($a);
That's basically what the third method is doing. Not sure why you included an embedded pass by reference.
Pass by reference (for scalars and arrays) should generally be avoided because it's less clear than returning the value. However, it can be useful in cases where you need to modify multiple values within one function call.
Note that in PHP5, there's not even a need to explicitly pass an object by reference if you simply want to modify the original object, as the handle to the object will point to the same object as was passed to the function.
The last example is not equivalent to the first two. If you print the value of $a after calling foo, you will see that it is not defined. The third is basically an obfuscated no-op.
The value of a local variable of a function has to be retained over multiple calls to the function. How should that variable be declared in PHP?
using static. example:
function staticDemo() {
static $x =1;
echo $x;
$x++;
}
the variable $x gets that static value the first time the function is called, afterwards it retains it's state.
example:
staticDemo(); // prints 1
staticDemo(); // prints 2
staticDemo(); // prints 3
Just FYI:
function globalDemo() {
global $x;
echo $x;
$x++;
}
globalDemo(); // prints ''
globalDemo(); // prints 1
globalDemo(); // prints 2
globalDemo(); // prints 3