<?php
// This is a PHP program that tests global scope
$globalVar = "Hello, World!"; // A global variable
function testScope() {
global $globalVar; // Declare the global variable inside the function
echo $globalVar; // Print the value of the global variable
}
testScope(); // Call the function to test the global variable
?>
Hello, World!
$globalVar
` is declared outside of the `testScope()
` function. The function then declares the same variable as global using the `global
` keyword, which allows it to access the global variable from within the function.$globalVar
` is printed to the screen using the `echo
` command. When the function is called at the end of the program, it outputs the value of the global variable "Hello, World!
".global
` keyword. However, it is generally recommended to avoid using global variables whenever possible, as they can make code more difficult to understand and maintain.