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. For example:


$x = "outside";

function test()
  global $x;
  echo $x;


Watch a 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.