W3docs

Can't access global variable inside function

In PHP, if you want to access a global variable inside a function, you need to use the "global" keyword.

Tags

In PHP, if you want to access a global variable inside a function, you need to use the "global" keyword. For example:

Example of accessing a global variable inside a function in PHP

php— editable, runs on the server
Info

Watch a video course: Learn object oriented PHP

This will output "outside". Without the "global" keyword, the function would not be able to access the variable $x and would produce an error.