DevelopPHP
home gear home donate
Search Results For
in:

Libraries book

HTML Library CSS Library JavaScript Library PHP Library

Videosbook

PHP and MySQL JavaScript HTML and CSS Vector and 3D Flash Actionscript Android Dev Miscellaneous

Resourcesbook

Website Hosting Developer Q+A
Global Variables   Share / Like
Global variables are set by a PHP script author to assure that a variable and its value will be available inside of a function, or when another PHP file is called to manipulate that variable. When you create custom variables in the normal fashion those variables and their values will not be available to you inside of a function or an externally called file. To make sure they are available you create the variable in the normal way and then apply the following code to make global and available to cover more scope only when needed.

Learn HTML
<?php
// Define your variable and its value
$flavor1 "chocolate";
// Now set it as global
global $flavor1;
//-------------------- EXTRA INFO ---------------------------------
// You can also set many variables as global together by using:
global $flavor1$flavor2$flavor3;
?>


Here is a code example showing where and why applying global to a variable is handy.
Learn HTML
<?php
// Define your variable and its value
$name1 "Joe";
// Create a simple function that will display a sentence
function sampleFunction() {
    
// Set $name1 variable as global
    
global $name1;
    
// Now echo a sentence to the browser
    
echo "Hello $name1, welcome to my  web page.";
}

// This is how you can execute a function to run, we talk more about functions later
sampleFunction();

?>

Develop PHP browser display window
Hello Joe, welcome to my web page.


Predefined Variables (aka: superglobals)
There are a set of predefined variables in PHP that are always available for use in any script or scope in your applications. When data is created, sent, or stored using mechanisms that create superglobal variables, we can then access those variables sitewide or all throughout our scripts and scope. Predefined superglobals are available to all of your scripts that are part of a website or application you are creating.

The superglobal variables are:
$_GET Stores any variables created using the GET method
$_POST Stores any variables created using the POST method
$_REQUEST Stores any variables created through a user input script (it can access both POST or GET)
$_FILES Stores any file upload variables created through user input scripts
$_SESSION Stores any variables created through registering session variables
$_COOKIE Stores any variables created through setcookie
$GLOBALS Stores any variables that have been globally defined
$_SERVER Stores server information such as headers, file names, reference paths, and current page.
$_ENV Stores any variables associated with the server environment.

Here is an example of accessing superglobal variables for the server:
Learn HTML
<?php
//Obtain user IP address 
$ip $_SERVER['REMOTE_ADDR']; 
// Obtain browser
$browser $_SERVER['HTTP_USER_AGENT'];
// Obtain user system language
$language $_SERVER['HTTP_ACCEPT_LANGUAGE'];
// Obtain the URL of the page that they came from
$referingURL $_SERVER['HTTP_REFERER'];
// Obtain the page they are currently on
$currentPage $_SERVER['REQUEST_URI'];

// Now show all of that information on page
echo "$ip <br />";
echo 
"$browser <br />"
echo 
"$language <br />";
echo 
"$referingURL <br />"
echo 
"$currentPage <br />";
?>


Superglobal variables are created and defined in different ways. We will demonstrate these methods throughout our lessons, we felt that while we are discussing variables we should tell you about all of the different types available to you and your scripts.


Home   •   Terms of Use   •   T-Shirts   •   RSS   •   Testimonials   •   Subscribe   •   Classroom Chalkboard   •   Developer Q+A   •   Donate   •   Top ↑
Popular In PHP / MySQL E-Commerce Store Production
Social Network Website Building
Image Upload / Photo Processing
CMS Software Programming
Mass Email Systems
Magic XML Data
Search Programming
Popular In JavaScript JSON Programming
Ajax Programming
Animating Elements
WYSIWYG Programming
Date/Time Programming
DOM Scripting
Object Reference
Popular In HTML Canvas Element
New Form Elements
Audio Element
Video Element
Drag and Drop
Event Handling
Element Grouping
Popular In CSS Custom Font Embedding
Dynamic Fit Backgrounds
Theatre Mode
Box Overlays
CSS Level 1 Properties
CSS Level 2 Properties
CSS Level 3 Properties
DevelopPHP.com is a growing educational system packed with video and written material that you can access 24/7 100% free. The focus here in 2013 is spread between five technologies: HTML, CSS, JavaScript, PHP and MySQL. Learn programming theory, database interaction, web design, animation, graphics editing, vector art, 3D modeling, and much more. The educational experience here is a hybrid one offering both high quality video tutorials and code libraries that grow more robust every day. The code libraries are being assembled so that developers can discover all of the building blocks of their favorite languages. The libraries coupled with high quality video tutorials will take you deep down the rabbit hole very quickly. You can also ask questions and get free advice at WebIntersect.com.
©2014 DevelopPHP.com HTML5