Don't muddy up the $_GLOBAL
space. If you want to nix the class route and use a function with a statically scoped variable you can keep it contained.
// getter + setter function
function prefix_my_var( $value = null ) {
static $s = null;
if ( ! is_null( $value ) ) {
$s = $value;
}
return $s;
}
// set + get
$value = prefix_my_var( 'foo' ); // foo
// set
$value = prefix_my_var(); // foo
Now if you pass a value it'll set+get it. And if you don't pass a value it'll just get the current value.
echo "<pre>";
print_r( array (
prefix_my_var(),
prefix_my_var( 'foo' ),
prefix_my_var(),
prefix_my_var( 'bar' ),
prefix_my_var(),
prefix_my_var( 'baz' ),
) );
Array
(
[0] =>
[1] => foo
[2] => foo
[3] => bar
[4] => bar
[5] => baz
)