global
` keyword to access a global variable from within a function :<?php
// This is a PHP program that uses the global keyword to access a global variable from within a function
$globalVar = "Welcome to Free Time Learn"; // 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
?>
Welcome to Free Time Learn
$globalVar
` is declared outside of the `testScope()
` function. Inside the function, the `global
` keyword is used to declare the same variable as global, which allows it to access the global variable from within the function.$globalVar
` is then 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 "Welcome to Free Time Learn".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.