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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。