PHPglobal Keyword
Example
Use a global variable in a function:
<?php
$x = 5;
function add($y) {
global $x;
return $x + $y;
}
echo "$x + 5 is " . add(5);
?>
Try it Yourself »$x = 5;
function add($y) {
global $x;
return $x + $y;
}
echo "$x + 5 is " . add(5);
?>
Definition and Usage
Theglobal keyword imports variables from the global scope into the local scope of afunction.
Related Pages
Read more about Variable Scope in ourPHP Variables Tutorial.
❮ PHP Keywords

