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 are the potential pitfalls when working with multipart MIME types in PHP?
- What are some common pitfalls to avoid when working with PHP to display content in specific sections of a webpage using include statements?
- How can the use of preg_replace() and mb_convert_encoding() functions in PHP affect the display of usernames with emojis and special characters?