Changing a global variable from inside a function PHP

In PHP, global variables can be accessed and modified from within functions using the global keyword. For example, to access a global variable called $x from within a function, you would use the following code:

<?php

$x = 10;

function modify_global()
{
  global $x;
  $x = 20;
}

modify_global();
echo $x; // outputs 20

Watch a course Learn object oriented PHP

This will change the value of the global variable $x to 5 within the function. Keep in mind that using global variables can make code harder to understand and maintain, and it is often better to pass variables as function arguments or use an object-oriented design.