- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
Advertisements
To Continue Learning Please Login
Login with Google