php 预定义超全局数组
100 讲 预定义超全局数组①- 原理分析 $_GET 更新时间 :2013 年 04 月 21 日 11 时 42 分 来源 : 现在我们做项目串知识点 . 先给大家讲解预定义超全局变量 PHP 中预定义超全局变量 ① 什么是全局变量 ?php $a=23;// 全局变量 , 因为该变量在整个作用域(在这
100讲 预定义超全局数组①-原理分析 $_GET
更新时间:2013年04月21日11时42分来源:
现在我们做项目串知识点.
先给大家讲解预定义超全局变量
PHP中预定义超全局变量
① 什么是全局变量
$a=23;//全局变量,因为该变量在整个作用域(在这个php文件中),都是可见
function test(){
//如果希望使用到全局$a;
global $a;
$a=45;
}
test();
echo $a;
?>
② 什么是超全局变量
超全局变量除了有全局变量的特点外,可以无须申明,直接使用.
快速入门
//超全局变量
$_SERVER['hsp']="韩顺平";
echo "";<br>
echo print_r($_SERVER);<br>
echo "
提供超全局数组的原因是为了让程序员更方便快捷的写出PHP程序.
③ PHP供提供了九种
1. $_GET
2. $_POST
3. $_REQUEST
4. $_SERVER
5. $_ENV
6. $_FILE
7. $_COOKIE
8. $_SESSION
9. $GLOBALS
现在一个一个讲解
1. $_GET
以$_GET原理来分析数据的来龙去脉...
说明原理图:
3 分钟前上传
下载附件 (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 "
//如果希望指定取出某个数据,则可以通过下标名
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 "
//如果希望指定取出某个数据,则可以通过下标名,如果php版本低,可能看到是中文,需要使用
urldecode() 来解密
echo "--".$_GET['city'];
?>
_GET也可以接收表单以get方式提交数据
预定义超全局数组②-$_POST $_REQUEST
$_POST
描述 : 通过Http POST方式提交的数据,会被封装到$_POST超全局数组中.
举例: 接收表单信息:
代码:
1 分钟前上传
下载附件 (26.82 KB)
代码:
register1.php页面
用户注册
用户名:
密码
性别:
你喜欢什么:
你的所在地是:
个人介绍 :
你选择图片
**regiseter2.php***
//看看如何接收
echo "";
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方式提交给ok.php
header(“Location: ok.php?aaa=xiaoming”);
?>
post和get区别请参看ppt
① 安全性 post>get
② 数据传输大小 [浏览器]post>get
③ 保存到收藏夹 get比较方便.
$_REQUST
定义: $_REQUST 中可以包括$_GET/$POST/$_COOKIE 数组,的信息.
快速入门:
这里就很好理解,看一个案例 :
echo "";
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 "";
echo print_r($GLOBALS);
echo "";
?>
当你定义一个全局变量,该变量也会被$GLOBALS管理
$a=900;
echo “”;
print_r($GLOBALS);
echo “”
?>
案例说明:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
