更新时间: 2017-01-07 16:02:52       分类: web开发


全局变量

引例

<?php
  $a = 23;//定义了全局的变量$a

  function test(){
    $a = 2;//函数内部的变量,
    echo $a;//输出是2
    //如果希望在函数内使用的$a是定义的全局变量,需要使用下面这句来声明其全局性:
    //global $a;
  }

  echo $a;//输出23
?>

超全局数组

特点

超全局数组是由系统定义的,不需要声明就可以直接使用的数组,在PHP中有9个这样的数组:

$_SERVER[];
$_REQUEST[];
$_GET[];
$_POST[];
$_ENV[];
$_COOKIE[];
$_SESSION[];
$_FILE[];
$_GLOBALS[];

GET

POST

REQUEST

SERVER

ENV?

FILE

COOKIE

SESSION

GLOBALS

一个二维数组,包含了几乎所有的超全局变量数组,如果你自己定义了一个全局变量,该变量也将自动归给$_GLOBALS[]管理

运行原理

$_GET[]为例,来分析超全局数组的工作方式。

  1. 浏览器访问一个超链接:http://xx//a.php浏览器将这个访问请求发送给apache服务器。

  2. apache解析访问的页面,是php脚本,就把脚本发送给php模块去处理。

  3. php模块为这个请求生成一个进程,把请求的相关信息封装成超全局数组供其使用

  4. php进程运行并返回结果(静态页面),然后发送给apache,再由apache返回给用户的浏览器

误区分析

  1. 可以向超全局数组中添加键值对,但是在某个脚本当中添加的只能在该脚本中使用。

  2. 服务器端每接收一次请求就会维护一组超全局数组,换句话说,每个进程都由php为其维护的一组超全局数组

  3. 低版本IE用get传送多个数据,其中某个包含中文,如果中文的数量为奇数个就会出现bug。

简单的解决办法是,在传送参数的时候,中文后面跟一个全角的空格,然后再客户端处理时利用trim()来删除。比如a.php?city=哈尔滨 &name=xiaoni

一个较好的方法是先用urlencode()对要转制的中文字符进行一次转码再放进去,取出时候用urldecode()解码

  1. 如果获取复选框(checkbox)的多个数据?

必须在name 一栏用[]声明是一个数组,否则php接收到的对应的复选框的数据只会保留最后一个。


评论
by RobertMeX   at   2017-01-12 09:05:46

The best fitness tip for building up your arms is to work opposite muscles in opposite sets of each other. The best example would be to work the triceps and then the biceps. Each has the opportunity to rest while the other is being worked. This minimizes your time and maximizes your workout. <a href=https://www.acheterviagrafr24.com/>https://www.acheterviagrafr24.com/</a>

by Angeloaneby   at   2017-10-02 06:28:40

As you will inevitably learn on your path to losing weight, effective weight loss is not only about watching what you eat, but much more about changing your lifestyle. This means changing your habits and how you approach your day-to-day life. Read this information to help you throughout the process.

by Robertnem   at   2017-12-03 21:46:42

Doctor Who is now considered a British Institute and has come a long way since it first aired on November 23rd 1963. The very first show saw the Doctor travel 100,00 years into the past to help some dim cavemen discover light. After 26 seasons and seven Doctors later the series came off our screens in 1989 much to the disappointment of the huge devoted fanbase. In 1996 an attempt was made to revive Doctor Who but it wasnt until June 2005 when it came back with a vengeance with Christopher Eccleston as the ninth Doctor that put the series back on the map as it were. It then went on for 5 years with David Tenant portraying the Doctor until 2010 when Matt Smith took over the role. Today it is still a great family show and has attracted many new fans. <a href=https://www.cialissansordonnancefr24.com/generiques-cialis/>https://www.cialissansordonnancefr24.com/generiques-cialis/</a>