Global is used to declare a superglobal variable that can be accessed in all scopes.
Index
$GLOBALS is used to access global variables in PHP. We can use $GLOBALS in all methods and functions to access global variables within the PHP script.
Sometimes we have to access global variables in a particular function or method without passing it as an argument. At that time, we can use $GLOBALS superglobal.
All global variables are stored in $GLOBALS array in PHP. We can access any global variable in the PHP script via its variable name in the $GLOBALS variable.
$GLOBALS['variable_name'];
Example 1
<?php $name = "errorsea.com"; function globals_example() { $GLOBALS['result'] = "Visit ".$GLOBALS['name']." to learn amazing programming concepts meticulously"; } globals_example(); echo $result; ?>
We can also access array variables via $GLOBALS.
Example 2
<?php $links = ["errorsea.com","google.com"]; function globals_array_example() { $GLOBALS['result'] = "Visit ".$GLOBALS['links'][0]." to unveil fascinating coding skills rather than searching at ".$GLOBALS['links'][1]; } globals_array_example(); echo $result; ?>
We can access any variable within the PHP script in any function or method. In addition, we can also declare a global variable in a method or function without passing any reference.
I hope you found this post interesting.
Enjoy Coding 🙂
There is a reason big-name companies like CNN use WordPress. WordPress is a popular content…
In this tutorial, I'm going to show you how to install MySQL on your computer.…
Download Turbo C++ for windows 10 in just 7 Mb and run your first C++…
We can redirect any webpage to any other or redirect the whole domain or website…
There are lots of methods to redirect pages, like refresh-redirect from META tag, redirect from…
Include files in PHP are used in appending various global or config files. We can…