PHP variable scope
● local
● global
● static
● parameter
Local scope, global scope
Copy after login
PHP global keyword
The global keyword is used to access global variables within a function.
To call global variables defined outside the function within a function, you can add the global keyword before the variables in the function.
Copy after login
PHP stores all global variables in an array called $GLOBALS.
So the above code can be written in another way:
Copy after login
PHP Static scope
PHP When a function completes, all its variables are usually will be deleted. In order to prevent some local variables from being deleted, you can use the static keyword when declaring the variable for the first time.
Copy after login
Parameter scope (formal parameter)
Parameter declaration as part of the function declaration.
Copy after login
Small addition:
Print array method:
echo ''; print_r($arr);Copy after login
Related recommendations:php tutorial
The above is the detailed content of The four major scopes of PHP variables. For more information, please follow other related articles on the PHP Chinese website!