PHP Global Variable

Unlike local variables, a global variable can be accessed in any part of the program. If we want a variable to be accessible from within a function, we can use the global keyword.

Global Variable can be accessed anywhere in our script, whether inside or outside a function. In order to modify a global variable, it must be explicitly declared to be global in the function in which it is to be modified.

Superglobals

Superglobals are built-in variables which are always available in all scopes throughout the script and hence it is not required to declare global $variable to access them within functions or methods.

These superglobal variables are:

(1) $GLOBALS - It references all variables available in global scope.

Example

<?php
function gvalue(){
   $animal = "cow"; 
   echo 'local variable:' .$animal."\n";
   echo 'globle variable:'.$GLOBALS[$animal]."\n";
}
$animal="horse";
gvalue();
?> 

This will produce following result

local variable: cow
globale variable: horse
(2) $_SERVER - It is an array which contains information like headers, paths, and script locations.

Example

<?php
	echo $_SERVER['SERVER_NAME'];
	echo "<br>";
	echo $_SERVER['HTTP_HOST'];
	echo "<br>";
?> 

This will produce following result

localhost/tslocal
localhost/tslocal
(3) $_GET - It is an associative array of variables passed to the current script through URL parameters.

Example

<?php
	echo 'Myself' . htmlspecialchars($_GET["myname"]) . '!';
?> 

Suppose the user has made an entry http://example.com/?myname=Bill

This will produce following result

Myself Bill!
(4) $_POST - It is an associative array of variables passed to the current script through HTTP POST method.

Example

<?php
	echo 'I want to meet Mr.' . htmlspecialchars($_POST["hostname"]) . '!';
?> 

Suppose the user has made an entry posted hostname = Jim

This will produce following result

I want to meet Mr. Jim
(5) $_COOKIE - It is an associative array of variables passed to the current script through HTTP Cookies.

Example

<?php
	echo 'Myself' . htmlspecialchars($_COOKIE["myname"]) . '!';
?> 

Suppose the "myname" cookie has already been set

This will produce following result

Myself Bill!


 
SHARE THIS IF YOU LIKE!
submit to reddit

SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.