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
Loading...
Related Quiz
- Is it possible to extend the execution time of a PHP script?
- The filter_input_array() function is used to get multiple input values and optionally filter them in PHP.
- What are the different types of encryption algorithms available in PHP? Explain their differences and use cases.
- How can you destroy a session in PHP?
- How can we pass a variable through navigation between pages?