Change A Static Variable Outside Its Function

<?php

function &foo() {
    static $x = [];
    print_r($x);

    return $x;
}

$a = foo()[] = 3;
var_dump($a);
foo();

Static variables are persistent between calls to the same method. They are local, unless they are returned with a reference. Then, they can be accessed in the main code, and read or even written.

This is the case here, without even storing the value: it is actually updated on the fly with the array notation on the function call.

See Also

PHP Features

Last updated: 14 July 2026