duancheng7743 2017-01-07 18:00
浏览 67

理解PHP $ GLOBALS变量

I am learning PHP from w3schools' PHP Tutorial.

While learning PHP I came across the concept of predefined global variables i.e. Superglobals.

In a curiosity to understand "Superglobals" more deeply I wrote the following code and executed it in a browser on my local machine(i.e.localhost) :

<!DOCTYPE html>
<html>
  <body>

  <?php
    echo "<pre>";
    print_r($GLOBALS);
    echo "</pre>";
  ?>

  </body>
</html>

I got following output in a browser :

Array
(
    [_GET] => Array
        (
        )

    [_POST] => Array
        (
        )

    [_COOKIE] => Array
        (
            [toWorkNormally] => 1
        )

    [_FILES] => Array
        (
        )

    [GLOBALS] => Array
 *RECURSION*
)

The above output has created many doubts in my mind as follows :

  1. As per my knowledge in PHP there are nine types of superglobals (predefined PHP global variables) viz. $GLOBALS, $_SERVER, $_REQUEST, $_POST, $_GET, $_FILES, $_ENV, $_COOKIE and $_SESSION then my doubt is what does the array elements from the predefined global array $GLOBALS viz. [_GET], [_POST], [_COOKIE], [_FILES] mean as they have their own independent existence as superglobals?
  2. What is meant by [toWorkNormally] => 1 from above array output?
  3. What does mean by RECURSION in element [GLOBALS] and how to print those elements?
  4. As the purpose of $GLOBALS array is to store variables declared by user globally then how this array has been pre-populated with some other values as I haven't declared any global variable in my code?

Note : I'm using "Microsoft Windows 10 Home Single Language" operating system on my machine. It's a 64-bit Operating System. I'm using latest edition of XAMPP with PHP 7.0.13 and HTTP Apache web server v.2.4.23 for running the program locally. Also, please note that I have not defined any other variable as global or local in my code.

  • 写回答

4条回答 默认 最新

  • doupa9062 2017-01-07 18:16
    关注

    $GLOBALS is the global of all super global and user defined variables. So for example if you have declared variable $a = 10; in your $GLOBALS array you have key=>value pair where key is a and value is 10.If you want to get something from $GLOBALS you just need to write it as array key.

    example

    $a = 25;
    echo $GLOBALS['a'];
    

    In the example above output will be the value of $a so 25;

    In your example toWorkNormally=>1 it`s mean that you have set cookie with name toWorkNormally and with value 1 or true

    Also when you submit form with get or post method in the $GLOBALS['_GET'] or $GLOBALS['_POST'] there you can find your form data as you can get them from super global $_GET or $_POST

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作