Variable Scope

The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single or local scope. This single scope spans included and required files as well.

<?php
$a = 1;
include 'b.inc';
?>

Here the $a variable will be available within the included b.inc script. However, within user-defined functions a local function scope is introduced. Any variable used inside a function is by default limited to the local function scope.

Global scope means a variable is available for whole document or site all the time.

Local Scope means a variable is available within limits in a function or an area.

Global Variables: $GLOBALS

An associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array.

This is a ‘superglobal’, or automatic global, variable. This simply means that it is available in all scopes throughout a script. You don’t need to do a global $GLOBALS; to access it within functions or methods.

You may specify a PHP array containing “global variables”.

<?php
$global_vars = array(); // This array must be associative
?>

For instance, let’s say you want to define a weblog name and a Template Group name. You could use this:

<?php
$global_vars = array("my_weblog_name" => "news","my_template_group" => "display");
// Last entry must not include the comma at the end
?>
  • Share/Bookmark

,

Leave a Review

Incoming Searches

php global scope, php variable scope global, php scope global, php variable scope, global scope in a php class, php scope only within file, php loop scope, php local file variable, php include with automatic global variables, php include array scope, php how to share variables inside file, php how to get a global scope, php globals variable scope, php scopes global, php var scoping, variable scope php, php whole website global variable, php while loop global variable, php variables in loop global, php variable tracking software, php variable scope while loop, PHP variable scope of include, php variable scope included files, php variable scope in while loop, php variable scope global prefix, php variable scope class array, php global variables scope, php global variables not working, php global variable within loop, how to use global variable in php while loop, how to access user defined global variable in php, how does global scope php, How do i References all variables available in global scope in net, global variable PHP class, global variable in php classes, global scope vs local scope, global scope php variables, global scope php, global and local variables in php classes, local and global variables in php, local global variable names, loop global php, php global scope variable, php global array, php for loop scope variables, php create automatic global variabile, php class scope global, php basics global, PHP arrays global, php array global scope, PHP array global, php acess a globalvariable, function scope global php