You have a PHP script and you need to modify a global variable from within a function. How would you do this using the $GLOBALS superglobal?

  • Use the 'global' keyword followed by the variable name to declare it as global within the function and then modify its value.
  • Assign a new value directly to the variable using the $GLOBALS array and the variable name as the key.
  • Use the 'static' keyword followed by the variable name to declare it as static within the function and then modify its value.
  • Use the 'return' statement to return the modified value to the calling code, which can then update the global variable.
To modify a global variable from within a function using the $GLOBALS superglobal, you can use the 'global' keyword followed by the variable name to declare it as global within the function. After declaring it as global, you can modify its value directly within the function. This way, the changes will be reflected in the global scope. Learn more: https://www.php.net/manual/en/reserved.variables.globals.php
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *