Table of Contents
  预定义超全局数组②-$_POST $_REQUEST
用户注册
 预定义超全局数组③-$_SERVER $_ENV $GLOBALS
Home php教程 php手册 php 预定义超全局数组

php 预定义超全局数组

Jun 06, 2016 pm 07:54 PM
100 php overall situation point principle definition array

100 讲 预定义超全局数组①- 原理分析 $_GET 更新时间 :2013 年 04 月 21 日 11 时 42 分 来源 : 现在我们做项目串知识点 . 先给大家讲解预定义超全局变量 PHP 中预定义超全局变量 ① 什么是全局变量 ?php $a=23;// 全局变量 , 因为该变量在整个作用域(在这

100讲 预定义超全局数组①-原理分析 $_GET

更新时间:201304211142来源:

现在我们做项目串知识点.

先给大家讲解预定义超全局变量
        PHP
中预定义超全局变量

        什么是全局变量


        $a=23;//
全局变量,因为该变量在整个作用域(在这个php文件中),都是可见

        function test(){
                //
如果希望使用到全局$a;
                global $a;
                $a=45;
        }
        test();
        echo $a;

?>

        什么是超全局变量
超全局变量除了有全局变量的特点外,可以无须申明,直接使用.
快速入门

        //
超全局变量
        $_SERVER['hsp']="
韩顺平";
        echo "

";<br>
        echo print_r($_SERVER);<br>
        echo "
Copy after login
"

提供超全局数组的原因是为了让程序员更方便快捷的写出PHP程序.

        PHP供提供了九种

1.        $_GET
2.        $_POST
3.        $_REQUEST
4.        $_SERVER
5.        $_ENV
6.        $_FILE
7.        $_COOKIE
8.        $_SESSION
9.        $GLOBALS

        现在一个一个讲解
1.        $_GET

$_GET原理来分析数据的来龙去脉...

说明原理图:

分钟前上传

下载附件 (60.59 KB)



$_GET
实际使用.
        客户机(浏览器)通过超链接传送信息给服务器

案例1

test.php
页面
        //urlencode
编码
        echo "
传送数据";
?>

面试题: 请问如何处理超链接提交数据的中文乱码问题?
1.
对于高版本的ie 浏览器,无需处理
2.
对应 5.5 6.0 ie需要进行urlencode()编码处理
3.
尽量使用字母和数字.

a.php
页面

        echo "

";<br>
        echo print_r($_GET);<br>
        echo "
Copy after login
Copy after login
";

        //如果希望指定取出某个数据,则可以通过下标名
        echo $_GET['name'];

?>

看看ie5.5 ie 6的中文奇数乱码.

解决方法1.
给奇数中文加一个全角空格,然后到服务器接收是,去掉两头的空行 trim()
解决方法2:
使用urlencode urldecode函数

test.php页面

        //
这句话是对中文url编码.        
        $str=urlencode("
北京好");

        echo "
传送数据";
?>

//
a.php 页面中,正常接收。


        echo "
";<br>
        echo print_r($_GET);<br>
        echo "
Copy after login
Copy after login
";

        //
如果希望指定取出某个数据,则可以通过下标名,如果php版本低,可能看到是中文,需要使用 urldecode() 来解密
        echo "--".$_GET['city'];
        

?>

_GET
也可以接收表单以get方式提交数据
a.php method=get>
//....


特别说明如何提交表单数据建议使用POST,不要使用GET

使用细节:
       
如果判断是否收到值

a.        php

        if(empty($_GET['city'])){
               
                echo "
没有收到";
        }else{
                echo "
city";
                echo$_GET['city'];
        }

//
这个方法可以,参看,建议使用empty
if(!isset($_GET['city'])){
               
                echo "
没有收到hello";
        }else{
                echo "
city";
                echo$_GET['city'];
        }

       
绝对不要启用register_globals  需要 register_globals = Off

  预定义超全局数组②-$_POST $_REQUEST

 

   $_POST

描述 : 通过Http POST方式提交的数据,会被封装到$_POST超全局数组中.

举例: 接收表单信息:

代码:


分钟前上传

下载附件 (26.82 KB)

代码:

register1.php页面

用户注册

用户名:

密码

性别:

你喜欢什么:

唱歌

跳舞

游泳

骑马


你的所在地是:

北京

天津

南京


个人介绍 :


你选择图片

**regiseter2.php***

      //看看如何接收

      echo "

";
Copy after login
Copy after login
Copy after login

      echo print_r($_POST);

      echo "";

      //关心爱好

      $name=$_POST['username'];

      $pwd=$_POST['passwd'];

      //如何接收checkbox提交的数据

      $hobbies=$_POST['hobby'];

      echo "个人信息如下
";

      echo "$name--$pwd";

      //print_r($hobbies);

      echo "该人的爱好";

      foreach($hobbies as $key=>$val){

             echo"
$key=$val";

      }

      echo "
你的所在地是";

      $city=$_POST['city'];

      echo $city;

      echo "
个人介绍如下";

      $intro=$_POST['intro'];

      echo "
$intro";

     

      echo "查看调查情况"

?>

这里还有一个下拉框的演示

10min:

你编写生活幸福度的调查表,然后把调查的信息,存入到数据库.,看看能否获取数据,显示给用户看.

get数据提交主要是

  默认是get提交

      //当这样的方式跳转的时候,也是以get方式提交给ok.php

      header(“Location: ok.php?aaa=xiaoming”);

?>

     post和get区别请参看ppt

① 安全性  post>get

② 数据传输大小 [浏览器]post>get

③ 保存到收藏夹 get比较方便.

     $_REQUST

定义: $_REQUST 中可以包括$_GET/$POST/$_COOKIE 数组,的信息.

快速入门:

这里就很好理解,看一个案例 :

      echo "

";
Copy after login
Copy after login
Copy after login

      echo print_r($_REQUEST);

      echo "";

      //如何取出某个值

      //$_REQUEST既可以接收get  请求数据,也可以接收post请求、cookie..

      //所以这个变量,比较不可以信,不建议使用. $_GET $_POST

      echo $_REQUEST['aa'];

      //如果不确定get/post

      if($_SERVER['http_method']=="post"){

             $echo $_POST['名字'];

      }else if($_SERVER['http_method']=="get"){

             //用 _GET接收.

      }

?>

注意事项:

1.     不要经常使用$_REQUEST,

2.     如果不确定_GET/POST

你接收数据时候,可以这样

if($_SERVER['REQUEST_METHOD’]=="POST"){

             $echo $_POST['名字'];

      }else if($_SERVER[’ REQUEST_METHOD’]=="GET"){

             //用 _GET接收.

      }

 预定义超全局数组③-$_SERVER $_ENV $GLOBALS

 

    $_ENV

该全局数组可以获取环境变量,在php5.3中默认禁用,但是你可以启动,php.ini文件中启用.

variables_order="EGPCS"

特别说明一把,在开发和生产环境,不要启用.

    $_FILE/ $_COOKIE /$_SESSION

后面讲

    $GLOBALS

包含了全部变量的全局组合数组。变量的名字就是数组的键[手册]

同时一个自定义的全局变量,也会自动的被$_GLOBALS 管理

案例:

      echo "aaaa";

      echo "

";
Copy after login
Copy after login
Copy after login

      echo print_r($GLOBALS);

      echo "";

?>

当你定义一个全局变量,该变量也会被$GLOBALS管理

      $a=900;

      echo “

”;
Copy after login

      print_r($GLOBALS);

      echo “”

?>

案例说明:

 

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles