Home  >  Article  >  Backend Development  >  Android programmers learn PHP development (22)-Super global array/super global variable-PhpStorm

Android programmers learn PHP development (22)-Super global array/super global variable-PhpStorm

黄舟
黄舟Original
2017-03-03 09:38:561279browse

PHP Super Global Array, also known as PHP predefined array, is built into the PHP engine and does not need to be redefined by developers. When a PHP script runs, PHP automatically places some data in a super global array.

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;.

php super global variable list:

$_GET[] Get the variable array submitted with the GET method

$_POST[] Get the variable array submitted with the POST method

$_COOKIE[] Get and set the cookie identifier of the current website

$_SESSION[] Obtains the unique identifier of the current user's access, expressed in the form of an array, such as sessionid and custom session data

$_ENV[] Current PHP environment variable array

$_SERVER[] Current php server variable array

$_FILES[] Parameter values ​​submitted to the current script when uploading files, reflected in the form of an array

$_REQUEST[] Contains all requests submitted by the current script, all actions of $_GET, $_POST, $_COOKIE

$GLOBALS[] Contains the reference content of all super global variables of the executing script

index.php:

<?php
    /**
     * 超全局数组/超全局变量
     * PHP 中的许多预定义变量都是“超全局的”,这意味着它们在一个脚本的全部作用域中都可用。
     * 在函数或方法中无需执行 global $variable; 就可以访问它们。
     * 这些超全局变量是:
        $_GLOBALS       Global变量
        $_SERVER        服务器变量
        $_REQUEST       request变量
        $_POST      HTTP POST变量
        $_GET       HTTP GET变量
        $_FILES     HTTP文件上传变量
        $_ENV       环境变量
        $_COOKIE        HTTP Cookies
        $_SESSION       Session变量
     */

    echo getip();
    echo "<br>";

    echo getip2();
    echo "<br>";

    /**
     * 遍历$_SERVER数组
     */
    foreach ($_SERVER as $key => $value){
        echo "{$key}  =>  {$value} <br>";
    }

    /**
     * 服务器IP的函数--笔试0分的写法
     */
    function getip(){
        return $_SERVER[&#39;REMOTE_ADDR&#39;];
    }

    /**
     * 服务器IP的函数--正确写法
     */
    function getip2(){
        if (!empty($_SERVER[&#39;HTTP_CLIENT_IP&#39;])){
            return $_SERVER[&#39;HTTP_CLIENT_IP&#39;];
        }elseif (!empty($_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;])){
            return $_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;];
        }elseif (!empty($_SERVER[&#39;REMOTE_ADDR&#39;])){
            return $_SERVER[&#39;REMOTE_ADDR&#39;];
        }else{
            return &#39;未知IP&#39;;
        }
    }
    ?>

    /**
     * GET
     */
    <br>
    <a href="demo.php?action=add&id=5&name=admin">测试页面</a><br>

    /**
    * POST
    * nickname[]下标会自增,跟数组没有区别
    */
    <br>
    <form action="demo.php?nick=www&psw=yyy" method="post">
        nickname:<input type="text" name="nickname[]"/><br>
        nickname:<input type="text" name="nickname[]"/><br>
        nickname:<input type="text" name="nickname[]"/><br>
        nickname:<input type="text" name="nickname[9]"/><br>
        nickname:<input type="text" name="nickname[]"/><br>
        nickname:<input type="text" name="nickname[x]"/><br>
        nickname:<input type="text" name="nickname[]"/><br>
        username:<input type="text" name="name"/><br>
        age:<input type="text" name="age"/><br>
        sex:<input type="text" name="sex"/><br>

        <input type="submit" name="sub" value="提交">
    </form><br>

<?php
    /**
     * $_SESSION
     *
     * Session 函数:
     * http://www.php.cn/
     *
     * Session 开始、存储、终结:
     * http://www.php.cn/
     */
    echo &#39;---------- $_SESSION ----------<br>&#39;;
    session_start(); // 启动新会话或者重用现有会话
    $_SESSION[&#39;name&#39;]="hello";
    print_r($_SESSION); // 打印结果:Array ( [name] => hello )
    session_unset(); // 释放所有的会话变量
    session_destroy(); // 销毁当前会话中的全部数据, 但是不会重置当前会话所关联的全局变量, 也不会重置会话 cookie。
                        // 如果需要再次使用会话变量, 必须重新调用 session_start() 函数
    session_write_close(); // 结束当前会话并存储会话数据
    setcookie(session_name(),&#39;&#39;,0,&#39;/&#39;); // session_name — 读取/设置会话名称
                                        // setcookie() 函数向客户端发送一个 HTTP cookie。
    session_regenerate_id(true); // 在不修改当前会话中数据的前提下使用新的 ID 替换原有会话 ID
    echo "<br>";

    /**
     * $GLOBALS 引用全局作用域中可用的全部变量
     * 一个包含了全部变量的全局组合数组。变量的名字就是数组的键。
     */
    echo &#39;---------- $GLOBALS ----------<br>&#39;;
    echo "<pre class="brush:php;toolbar:false">";
    print_r($GLOBALS); // 打印以上所有结果
    echo "
";

demo.php: (used with index.php)

<?php
    /**
     * $_GET 只能接get
     */
    print_r($_GET);
    echo "<br>";
    print_r($_GET[&#39;action&#39;]);
    echo "<br>";

    /**
     * $_POST 只能接post
     */
    print_r($_POST);
    echo "<br>";

    /**
     * $_REQUEST 能接get和post
     * 但是,容易被黑客攻击,因为什么都能接
     * 所以需要get就用$_GET,需要post就用$_POST
     * 忽略$_REQUEST的存在
     */
    print_r($_REQUEST);
    echo "<br>";

    /**
     * 不确定get还是post
     * 可以使用下面的写法
     */
    $arr = !empty($_POST) ? $_POST : $_GET;

above It is the content of Android programmers learning PHP development (22) - super global array/super global variable - PhpStorm. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn