w3resource

PHP $GLOBALS (super global) variable

Description

$GLOBAL is a php super global variable which can be used instead of 'global' keyword to access variables from global scope, i.e. the variables which can be accessed from anywhere in a php script even within functions or methods.

Here is an example to testify the theory mentioned above:  

<?php
$s = 25;
$t = 50;
function subtraction()
{
$GLOBALS['v'] = $GLOBALS['t'] - $GLOBALS['s'];
} 
subtraction();
echo $v;
?> 

In the code above, since v is a variable present within $GLOBALS array, it is accessible form outside the function also.

View the example of $GLOBALS variable in the browser

Previous: Print
Next: $_SERVER



PHP: Tips of the Day

Output a Multidimensional Array with index and value and print into the table

Example:

Array
(
 [0] => Array
 (
 [id] => 13
 [category_id] => 7
 [name] => Leaving Of Liverpool
 [description] => Leaving Of Liverpool
 [price] => 1.00
 [virtual] => 1
 [active] => 1
 [sort_order] => 13
 [created] => 2007-06-24 14:08:03
 [modified] => 2007-06-24 14:08:03
 [image] => NONE
 )
[1] => Array
 (
 [id] => 16
 [category_id] => 7
 [name] => Yellow Submarine
 [description] => Yellow Submarine
 [price] => 1.00
 [virtual] => 1
 [active] => 1
 [sort_order] => 16
 [created] => 2007-06-24 14:10:02
 [modified] => 2007-06-24 14:10:02
[image] => NONE
 )
)
<table>
<?php
foreach ($products as $key => $value) {
 foreach ($value as $k => $v) {
 echo "<tr>";
 echo "<td>$k</td>"; // Get index.
 echo "<td>$v</td>"; // Get value.
 echo "</tr>";
 }
}
?>
</table>