PHP学习笔记:环境变量
1. 概述 PHP环境变量主要有: $GLOBALS[]:储存当前脚本中的所有全局变量,其KEY为变量名,VALUE为变量 $_SERVER[]:当前WEB服务器变量数组 $_GET[]:存储以GET方法提交表单中的数据 $_POST[]:存储以POST方法提交表单中的数据 $_COOKIE[]:取得或设置用户浏
1. 概述
PHP环境变量主要有:
$GLOBALS[]:储存当前脚本中的所有全局变量,其KEY为变量名,VALUE为变量值
$_SERVER[]:当前WEB服务器变量数组
$_GET[]:存储以GET方法提交表单中的数据
$_POST[]:存储以POST方法提交表单中的数据
$_COOKIE[]:取得或设置用户浏览器Cookies中存储的变量数组
$_FILES[]:存储上传文件提交到当前脚本的数据
$_ENV[]:存储当前WEB环境变量
$_REQUEST[]:存储提交表单中所有请求数组,其中包括:$_GET、$_POST、$_COOKIE和$_SESSION中的所有内容
$_SESSION[]:存储当前脚本的会话变量数组
2. $GLOBALS[]
GLOBALS是由已定义全局变量组成的数组,变量名就是数组的索引,eg:
$ENTER = "<br>"; $var1 = 5; $var2 = 10; print $GLOBALS['var1'] . $ENTER ; print $GLOBALS['var2'] . $ENTER ;
3. $_SERVER[]
_SERVER是一个包含了诸如头信息(header)、路径(path)、以及脚本位置(script locations)等信息的数组,eg:
$ENTER = "<br>"; print "SERVER_ADDR:" . $_SERVER['SERVER_ADDR'] . $ENTER;
参考:http://www.php.net/manual/zh/reserved.variables.server.php
4. $_GET和_POST$_GET和_POST用来接收请求数据,实现输入
$_GET内容通过 URL 参数传递给当前脚本的变量的数组。
$_POST内容是由 HTTP POST方法发送的变量名称和值。
GET是HTTP中最原始的请求方式,在网页中点击一个超级链接或在地址栏输入一个URL都会发送一个GET请求。在GET请求中,数据是后缀在URL后面来发送的,like:http://192.168.21.133/test1.php?name_get=zxm&age_get=23
POST方法的主要用途就是传递数据,它将数据放在所有请求标题的后面上传,这样一来,无论有多少数据上传都不成问题了
eg:
HTML:
//_GET echo "name:" . $_GET["name_get"] . $ENTER ; echo "age:" . $_GET["age_get"] . $ENTER; //_POST echo "name:" . $_POST["name_post"] . $ENTER ; echo "age:" . $_POST["age_post"] . $ENTER; ?>
5. $_FILE
通过$_FILES,我们可以从客户计算机向远程服务器上传文件。
上传文件表单:
//_FILE echo "Error: " . $_FILES["file"]["error"] . "<br>"; echo "Upload: " . $_FILES["file"]["name"] . "<br>"; echo "Type: " . $_FILES["file"]["type"] . "<br>"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br>"; echo "Stored in: " . $_FILES["file"]["tmp_name"];
cookie 是一种在远程浏览器端储存数据并以此来跟踪和识别用户的机制, 常用于识别用户,每当相同的计算机通过浏览器请求页面时,它同时会发送 cookie,通过$_COOKIE[],我们可以将cookie取出。
设置cookie:
<?php setcookie("user", "zxm"); ?>
//cookie if (isset($_COOKIE['user'])) echo "Welcome " . $_COOKIE['user'] . "!<br>"; else echo "Welcome guest!<br>";
_SESSION[]存储了有关用户会话的信息。
eg:
<?php session_start(); $_SESSION['id'] = "100"; ?>
//session if(isset($_SESSION['id'])) echo "session id: " . $_SESSION['id'] . $ENTER; else echo "without session" . $ENTER; ?>
php中的$_ENV存储了一些系统的环境变量,因环境不同而值不同.
8. _REQUEST[]
$_REQUEST包含了$_GET、$_POST、$_COOKIE的所有内容
$_GET、$_POST、$_COOKIE在$_REQUEST中都有一个副本。改变$_REQUEST的值不影响$_GET等,反之亦然。
在同名的情况下,优先级:$_GET EG:
//request echo "request: " . $_REQUEST['name_get'] . "!<br>"; ?>
"; //__GLOBALS[] $var1 = 5; $var2 = 10; print $GLOBALS['var1'] . $ENTER ; print $GLOBALS['var2'] . $ENTER ; //_SERVER print "SERVER_ADDR:" . $_SERVER['SERVER_ADDR'] . $ENTER; //_GET echo "name:" . $_GET["name_get"] . $ENTER ; echo "age:" . $_GET["age_get"] . $ENTER; //_POST echo "name:" . $_POST["name_post"] . $ENTER ; echo "age:" . $_POST["age_post"] . $ENTER; //_FILE echo "Error: " . $_FILES["file"]["error"] . $ENTER; echo "Upload: " . $_FILES["file"]["name"] . $ENTER; echo "Type: " . $_FILES["file"]["type"] . $ENTER; echo "Size: " . ($_FILES["file"]["size"] / 1024) . $ENTER; echo "Stored in: " . $_FILES["file"]["tmp_name"] . $ENTER; //cookie if (isset($_COOKIE['user'])) echo "Welcome: " . $_COOKIE['user'] . "!
"; else echo "Welcome guest!
"; //session if(isset($_SESSION['id'])) echo "session id: " . $_SESSION['id'] . $ENTER; else echo "without session" . $ENTER; //request echo "request: " . $_REQUEST['name_get'] . "!<br>"; ?>

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











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

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,

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.

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

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.

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
