Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Explain Function Calls with the static Statement in PHP ?

In PHP, the static statement is used within a function to allow the function to maintain its state between multiple calls.

When you use the static statement for a variable inside a function, the variable retains its value even after the function execution is completed.

This behavior is different from regular local variables, which are reinitialized with each function call.

Example to illustrate the use of the ‘static’ statement

PHP
function countCalls() {
    static $counter = 0;
    $counter++;
    echo "Function has been called $counter times.<br>";
}

countCalls(); // Output: Function has been called 1 times.
countCalls(); // Output: Function has been called 2 times.
countCalls(); // Output: Function has been called 3 times.

In the example, the variable $counter retains its value across multiple calls to the countCalls() function.

It starts with the initial value of 0 and increments by 1 with each function call, maintaining its state between the calls.