What does the line "global $variable" mean in PHP?
Using the line "global $variable" in PHP allows you to access a global variable within a function. This is necessary when you want to modify a global variable from within a function's scope. By using the "global" keyword, you can indicate that the variable being referenced is the global one, not a local variable with the same name.
$globalVar = 10;
function modifyGlobalVar() {
global $globalVar;
$globalVar += 5;
}
modifyGlobalVar();
echo $globalVar; // Output: 15
Keywords
Related Questions
- How can arrays be used to simplify the process of accessing and manipulating data in PHP?
- What steps can be taken to prevent unauthorized access to the server and protect sensitive information when using PHPMailer for sending emails?
- How important is it for PHP developers to have a clear understanding of timestamp concepts?