
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
PHP $GLOBALS
Introduction
$GLOBALS is an associative array of references to all globalle defined variables. Names of variables form keys and their contents are values of associative array.
$GLOBALS example
This example shows $GLOBALS array containing name and contents of global variables
Example
<?php $var1="Hello"; $var2=100; $var3=array(1,2,3); echo $GLOBALS["var1"] . "
"; echo $GLOBALS["var2"] . "
"; echo implode($GLOBALS["var3"]) . "
"; ?>
Output
This will produce following result. −
Hello 100 123
In following example, $var1 is defined in global namespace as well as a local variable inside function. Global variable is extracted from $GLOBALS array;
Example
<?php function myfunction(){ $var1="Hello PHP"; echo "var1 in global namespace:" . $GLOBALS['var1']. "
"; echo "var1 as local variable :". $var1; } $var1="Hello World"; myfunction(); ?>
Output
This will produce following result. −
var1 in global namespace:Hello World var1 as local variable :Hello PHP
- Related Articles
- The globals(), locals() and reload() Functions in Python
- What is difference between builtin and globals namespaces in Python?
- What is the difference between dir(), globals() and locals() functions in Python?
- PHP php://
- Upload file with php to another php server
- PHP Generators.
- PHP Overloading
- PHP Traits
- PHP $_GET
- PHP $_SERVER
- PHP superglobals
- PHP References
- PHP Constants
- PHP Expressions
- PHP Tags

Advertisements