PHP global Keyword

❮ PHP Keywords

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 »

Definition and Usage

The global keyword imports variables from the global scope into the local scope of a function.


Related Pages

Read more about Variable Scope in our PHP Variables Tutorial.


❮ PHP Keywords
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.