Can static variables in PHP functions retain their values between function calls? If so, how?
Yes, static variables in PHP functions can retain their values between function calls. This can be achieved by declaring a variable as static within a function. When a function is called, the static variable retains its value from the previous function call and can be modified and accessed within the function.
function increment() {
static $count = 0;
$count++;
echo $count . "\n";
}
increment(); // Output: 1
increment(); // Output: 2
increment(); // Output: 3
Keywords
Related Questions
- What considerations should be taken into account when incorporating PHP code to manipulate URLs within a script?
- How can the ID of the active page be accurately identified in a PHP script?
- Are there any best practices or recommendations for handling BOM-Byte related issues when working with XML files in PHP?