Home  >  Article  >  Backend Development  >  What are the global arrays in php

What are the global arrays in php

青灯夜游
青灯夜游Original
2022-05-17 15:42:302407browse

There are 9 php global arrays: 1. "$GLOBALS", which is an array composed of all defined global variables; 2. "$_SERVER", which contains header information, paths and script locations. Array; 3. "$_COOKIE", an array composed of session data; 4. "$_GET"; 5. "$_ENV", etc.

What are the global arrays in php

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

php global array

php The full name of the global array is "super global array" or "super global variable". It is a specially defined array variable in PHP. It is called super global array because these arrays are in the script. It can be accessed anywhere and in any scope, such as functions, classes, files, etc.

Superglobal array variables are built-in variables that are always available in all scopes.

The superglobal array in PHP includes the following 9:

  • $GLOBALS: Contains a reference to the global variable scope of each current script Valid variables within. The key names of this array are the names of global variables.

  • $_SERVER: Display or obtain server-related information;

  • $_GET: Pass data to the server through the address bar, this is the method Must be passed in $_GET mode;

  • $_POST: The data sent through the form must be in POST mode;

  • $_REQUEST: Contains the above two A data transfer method (POST and GET);

  • $_COOKIE: Process the client's session data;

  • $_SESSION: Process the server-side session Data;

  • $_FILES: Array needed to upload files;

  • $_ENV: Variables submitted by the execution environment to the script

##$_GET

    Principle

  • What are the global arrays in php
  • Usage 1: The browser sends information to the server through a hyperlink (the default is GET)

<meta>


//注意:信息如何传递!!!
<a>数据</a>

rrree
    Note: IE5. 5 and IE6 have the problem of Chinese odd numbers being garbled, how to solve it? urlencode and urldecode.

<meta>


<?php echo "<pre class="brush:php;toolbar:false">";
 	echo print_r($_GET);
	echo "
"; echo $_GET['name']; ?>

<meta>


<?php $city=urlencode("大西安");
	echo "<a href=&#39;1.php?city={$city}&name=tan&#39;>数据";
?>

    Use 2: The browser sends information to the server through the form (it is best to use $_POST)

<meta>


<?php echo "<pre class="brush:php;toolbar:false">";
 	echo print_r($_GET);
	echo "
"; echo urldecode($_GET['city']); ?>

<meta>


输入内容:
    Disable enabling register_globals: Set register_globals =off;
  • Enable register_globals. If you enter http://localhost:88/1.php?a=10&b=20; when accessing the following code, you will get 10 20.

<meta>




$_POST

    Data submitted through post will be encapsulated into the $_POST super global array .
  • Use 1: The browser transmits information to the server through the form (some data must use the post method)

<meta>





<meta>


用户名:
密码:
性别:
籍贯:
爱好:唱歌 跳舞 LOL
个人简介:
文件上传:
  • The difference between the POST method and the GET method

      POST method has good security
    • Data transmission size (POST method is large)
    • Save to favorites (GET method)

$_REQUEST

$_REQUEST is an array containing $_POST, $_GET and $_COOKIE. The array structure is the same as $_POST and $_GET similar.

That is to say, whether the data is submitted using POST or GET, you can use $_REQUEST to obtain it, and you can even use $_REQUEST to obtain COOKIE information. The request_order attribute was introduced in PHP5.3. We can control the content contained in $_REQUEST by modifying the value of the request_order attribute in the php.ini configuration file.

The value of the request_order attribute can be the three uppercase English letters G, P and C, which represent GET, POST and COOKIE respectively. By default, the default value of the request_order attribute is request_order="GP", which does not include C, which means that $_REQUEST does not contain COOKIE information. If we want $_REQUEST to contain COOKIE, we need to modify it to request_order= "GPC".

Note: Do not use $_REQUEST often (because multiple messages will be mixed).


<meta>


"; 
	//如何接收checkbox的所有数据(注意中括号[]):唱歌
	echo print_r($_POST);
	echo "
"; $hobbies=$_POST['hobby']; foreach($hobbies as $key=>$val){ echo "
".$key." ".$val; } ?> rrree
    Distinguish between GET and POST through $_SERVER['REQUEST_METHOD']

<meta>


超链接
用户名:
密码:

$_SERVER

$_SERVER is an array containing information such as header information, path and script location. The entities of the array are created by the web server, and there is no guarantee that all servers can generate all information. The server may ignore some information, or generate some other new information. Like other superglobal arrays, this is an automatic global variable that is available in all scripts. There is no need to use the global keyword to access it in functions or object methods.

$_ENV

获取服务器端的环境变量,通过修改php.ini的variables_order="EGPCS"启动。

$_ENV数组中的内容是在PHP解析器运行时,从PHP所在服务器中的环境变量转变为PHP全局变量的。它们中许多都是由PHP所运行的系统决定的,完整的列表是不可能的,需要查看PHP所在服务器的系统文档以确定其特定的环境变量。

$GLOBALS

$GLOBALS是由所有已定义的全局变量组成的数组,变量名就是该数组的索引。

  • 包含了全部变量的全局组合数组。

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

<meta>


";
	print_r($GLOBALS);
	echo "
"; ?>

What are the global arrays in php

$_FILES

使用表单的file输入域上传文件时,必须使用post提交。但在服务器文件中,并不能通过$_POST超全局数组获取到表单中file域的内容。而$_FILES超全局变量是通过post方法传递的已上传文件项目组成的数组。$_FILES是一个二维数组,包含5个子数组元素,其中第一个下标是表单中file输入域的名称,第二个下标用于描述上传文件的属性。

$_COOKIE

$_COOKIE超全局数组是经由HTTP Cookies方法提交至脚本的变量。通常这些Cookies是由以前执行的PHP脚本通过setCookies()函数设置到客户端浏览器中的,当PHP脚本从客户浏览器提取了一个cookie后,它将自动把它转换成一个变量,可以通过这个$_COOKIE超全局数组和cookie的名称来存取指定的cookie值。

$_SESSION

会话控制是在服务器端使用session跟踪用户,当服务器页面中使用session_start()函数开启session后,就可以使用$_SESSION数组注册全局变量,用户就可以在整个网站中访问这些会员信息。

说明:$_COOKIE、$_SESSION、$_REQUEST的区别

在 PHP 中,cookie 就是服务器,它是留在客户端(浏览器)上的一个小的数据文件,通常用于标识用户信息,也称为浏览器缓存或 Cookies。

$_COOKIE[] 全局数组存储了通过 HTTP COOKIE 传递到脚本的信息,PHP 可通过 setcookie() 函数设置 COOKIE 的值,用 $_COOKIE[] 数组接收 COOKIE 的值,$_COOKIE[] 数组的索引为 COOKIE 的名称。

session 是一种客户与网站(服务器)更为安全的对话方式,一旦开启了 session 会话,便可以在网站的任何页面使用(保持)这个会话,从而让访问者与网站之间建立了一种“对话”机制。但是 session 不同于 cookie,必须先启动,才能生效。

$_SESSION[] 数组用于获取会话变量的相关信息。

$_REQUEST 支持 $_GET 和 $_POST 发送过来的请求,即 get 和 post 它都可以接受,浏览器地址栏中的数据显示不显示要看传递的方法,get 会显示在 url 中(有字符限制),post 不会显示在 url 中,可以传递任意多的数据(只要服务器支持)。

默认情况下,$_REQUEST[] 数组包含了 $_GET、$_POST 和 $_COOKIE 的数组。

推荐学习:《PHP视频教程

The above is the detailed content of What are the global arrays in php. For more information, please follow other related articles on the PHP Chinese website!

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