By default, variables are limited to the scope they are defined in. If defined in a file outside of a function or class, they are limited to the scope of the file. If defined in a function or class, they are limited to the scope of the function or class.
<?php
$file_scoped = "I am file scoped";
function example(): void
{
$function_scoped = "I am function scoped!";
}
Using the keyword global
before a variable assignment creates a globally scoped variable. This pattern is often discouraged as it creates tightly-coupled code.
function example(): void
{
global $hello = "World!";
}
echo $hello; // #=> "World!"
Using the keyword static
before a variable assignment or declaration will preserve the variables scope, but its value is preserved between execution scopes.
function counter(): int
{
static $count = 0;
$count += 1;
return $count;
}
In this example, the $count
variable is initialized to 0 only on the first function call. Every call after, the value will be carried over to the next call. So every invocation of the function returns an increasing integer response.