Free Beginner PHP Tutorials - Global Functions and variables

2016:09:26 / Science and Tutorials
Tutorial how to create Global Functions and variables in PHP to referenced
inside a function
PHP Global Variables (Superglobals).
Note, Superglobals are variables available in all scopes and were introduced in PHP 4.1.0.
For this article, I will explain the two types of PHP superglobal variables.
-built-in global variables
-user-defined global variables
PHP built-in global variables
These are predefined PHP variables (built-in) and are accessible from any function or class regardless of their scope.
Some of the PHP built-in global variables are:
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION
user-defined global variables
These are user defined global variables which are declared as global from our function.
We use the keyword 'global' to declare our variables inside our function.
Example
<?php
$x=20;
$y=30;
function findProduct(){
global $x, $y;
$area = $x * $y;
echo"Our product is: ".$area;
}
findProduct();
?>
Output: Our product is: 600