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
- What is the purpose of using explode() in PHP and what are the potential pitfalls when using it in string manipulation?
- What role does proper error handling play in debugging PHP code, especially when dealing with syntax errors?
- When implementing email validation in PHP, what are some best practices to consider?