How can static variables in PHP be misused and what are the best practices for their usage?

Static variables in PHP can be misused by storing global state within functions, leading to unexpected behavior and difficult-to-debug code. To use static variables effectively, they should be used only when necessary for function persistence and should not be relied upon for global state management. Best practices include clearly documenting their purpose, limiting their use to specific cases where function persistence is required, and avoiding excessive reliance on them for shared state.

function incrementCounter() {
    static $counter = 0;
    $counter++;
    return $counter;
}

echo incrementCounter(); // Output: 1
echo incrementCounter(); // Output: 2