PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
In PHP, static variables have a property where their value is retained even after the function execution is over. This is different from normal function variables, which are deleted and their value is not retained once the function execution is over.
Here's a basic example to illustrate the difference:
function test() { $nonStaticVariable = 0; // This is a non-static variable static $staticVariable = 0; // This is a static variable $nonStaticVariable++; $staticVariable++; echo "non-static: $nonStaticVariable, static: $staticVariable<br />"; } test(); // non-static: 1, static: 1 test(); // non-static: 1, static: 2 test(); // non-static: 1, static: 3
In this code, we have a function test()
with a non-static variable $nonStaticVariable
and a static variable $staticVariable
. Both variables are incremented each time the function is called.
When we call test()
multiple times, we can see that $nonStaticVariable
is always 1
because it's reinitialized with 0
and then incremented each time test()
is called.
On the other hand, $staticVariable
retains its value between calls. It's initialized with 0
the first time test()
is called, and then each subsequent call to test()
increments the value of $staticVariable
from where it left off.
So, static variables in PHP are useful when you want a function to remember the value of a variable between calls. This is often useful in situations where you want to keep track of a count, as in the example above, or when you want to cache values that are expensive to compute.
Using static variables in PHP functions:
// File: static_variable.php function incrementCounter() { static $counter = 0; $counter++; echo "Counter: $counter"; } incrementCounter(); // Output: Counter: 1 incrementCounter(); // Output: Counter: 2
PHP static variable example code:
// File: static_example.php function trackVisits() { static $visits = 0; $visits++; echo "Visits: $visits"; } trackVisits(); // Output: Visits: 1 trackVisits(); // Output: Visits: 2
Static variables in object-oriented PHP:
self::
or ClassName::
.// File: StaticExampleClass.php class StaticExampleClass { public static $counter = 0; public function incrementCounter() { self::$counter++; echo "Counter: " . self::$counter; } } // Usage $instance1 = new StaticExampleClass(); $instance1->incrementCounter(); // Output: Counter: 1 $instance2 = new StaticExampleClass(); $instance2->incrementCounter(); // Output: Counter: 2
PHP static variables in loops:
// File: static_in_loop.php function countIterations() { for ($i = 1; $i <= 3; $i++) { static $iterations = 0; $iterations++; echo "Iteration $i: $iterations\n"; } } countIterations(); /* Output: Iteration 1: 1 Iteration 2: 2 Iteration 3: 3 */
PHP static vs non-static variables:
// File: static_vs_non_static.php function staticExample() { static $staticVar = 0; $nonStaticVar = 0; $staticVar++; $nonStaticVar++; echo "Static: $staticVar, Non-Static: $nonStaticVar\n"; } staticExample(); // Output: Static: 1, Non-Static: 1 staticExample(); // Output: Static: 2, Non-Static: 1