Many predefined variables in PHP are "superglobal," meaning they are available throughout the entire scope of a script. They can be accessed within a function or method without executing global $variable;.
These superglobal variables are:
- $GLOBALS
- $_SERVER
- $_GET
- $_POST
- $_FILES
- $_COOKIE
- $_SESSION
- $_REQUEST
-
$_ENV
1. Let’s take a look at $GLOBALS first. It is a global combination array that contains all variables. What does it mean? Let’s look at a C language program.
int main()
{
int a = 3;
void t()
{
printf("%d",a);
}
t();
return 0;
}
Copy after login
When this program is run, it will definitely output a. Is it easy to understand? The a variable is output in the t() function. But look at a program in php:
<?php
$a = 3;
function t(){
echo $a;
}
t();
?>
Copy after login
Will this output 3? no, no, no, that’s too naive, it can’t output anything, why??? It’s very simple, because $a is not a global variable, and its value cannot be distinguished in the t() function, wow ~ the world is dark What should I do? ? Don't be nervous, this is when our $GLOBALS comes into play. What I just said is a global combination array that contains all variables. Maybe you didn't understand it, but it should be much clearer now, that is to say, through $GLOBALS You can get the value of $a in the t() function using the method $GLOBAL['$A']. Try changing the content of the t() function to function t(){
echo $GLOBALS['a'];
}Run it and the value of $a will be clearly displayed on the page. Let's talk about the scope of $GLOBAL. It can get the values in your current page and the pages that require and include from the current page. It's pretty awesome. Of course, it’s not a panacea. Take another look at this
<?php
$a = 5;
function t(){
$b = 2;
}
function w()
{
echo $GLOBALS['b'];
echo $GLOBALS['a'];
}
t();
w();
?>
Copy after login
What is the output? ? ? The answer is only 5, which means $GLOBALS cannot get the values in other functions.
Looking at the second $_SERVER, $_SERVER is an array containing information such as header, path, and script locations. The items in this array are created by the web server. There is no guarantee that every server will offer all items; servers may ignore some, or serve items not listed here. A lot of useful information can be extracted from $_SERVER. For example, $_SERVER['REMOTE_ADDR'] can get the current user's IP. Next, I use foreach to traverse the entire $_SERVER array and print it. The code is as follows:
<?php
foreach($_SERVER as $key => $value){
echo "<b>$key:</b> $value<br>\n";
}
Copy after login
If you want to see the effect but don’t want to or can’t write now, you can check this URL. This is the effect of the sae server. http://5253.sinaapp.com/blog/server.php I uploaded it here.
http://www.bkjia.com/PHPjc/664281.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/664281.htmlTechArticleMany predefined variables in PHP are "superglobal", which means that they are used throughout a script Available in scope. There is no need to execute global $variable; in a function or method...