php global variables, php global variable definition
Jul 06, 2016 pm 02:25 PMphp global variables, php global variable definition
PHP global variables
Several superglobals are predefined in PHP, which means that they are available in the entire scope of a script. You can use it in functions and classes without special instructions.
PHP super global variable list:
- $GLOBALS
- $_SERVER
- $_REQUEST
- $_POST
- $_GET
- $_FILES
- $_ENV
- $_COOKIE
- $_SESSION
In this chapter we will explain several commonly used super global variables, and we will introduce the remaining variables in the next few chapters.
PHP $GLOBAL
$GLOBAL is a super global variable group of PHP, which can be accessed in the entire scope of a PHP script.
$GLOBAL is a global combined array containing all variables. The name of the variable is the key of the array.
The following example shows how to use the super global variable $GLOBAL:
<?<span>php </span><span>$x</span> = 75<span>; </span><span>$y</span> = 25<span>; </span><span>function</span><span> addition() { </span><span>$GLOBALS</span>['z'] = <span>$GLOBALS</span>['x'] + <span>$GLOBALS</span>['y'<span>]; } addition(); </span><span>echo</span> <span>$z</span><span>; </span>?>
In the above example, z is a super global variable in the $GLOBALS array, which can also be accessed outside the function.
PHP $_SERVER
$_SERVER is an array containing information such as header, path, and script locations. The items in this array are created by the web server. There is no guarantee that every server will offer all items; servers may ignore some, or serve items not listed here.
The following example shows how to use the elements in $_SERVER:
<?<span>php </span><span>echo</span> <span>$_SERVER</span>['PHP_SELF'<span>]; </span><span>echo</span> "<br>"<span>; </span><span>echo</span> <span>$_SERVER</span>['SERVER_NAME'<span>]; </span><span>echo</span> "<br>"<span>; </span><span>echo</span> <span>$_SERVER</span>['HTTP_HOST'<span>]; </span><span>echo</span> "<br>"<span>; </span><span>echo</span> <span>$_SERVER</span>['HTTP_REFERER'<span>]; </span><span>echo</span> "<br>"<span>; </span><span>echo</span> <span>$_SERVER</span>['HTTP_USER_AGENT'<span>]; </span><span>echo</span> "<br>"<span>; </span><span>echo</span> <span>$_SERVER</span>['SCRIPT_NAME'<span>]; </span>?>
Run
The following table lists the important elements in all $_SERVER variables:
元素/代碼 | 描述 |
---|---|
$_SERVER['PHP_SELF'] | 當前執(zhí)行腳本的文件名,與 document root 有關。例如,在地址為 http://example.com/test.php/foo.bar 的腳本中使用 $_SERVER['PHP_SELF'] 將得到 /test.php/foo.bar。__FILE__ 常量包含當前(例如包含)文件的完整路徑和文件名。 從 PHP 4.3.0 版本開始,如果 PHP 以命令行模式運行,這個變量將包含腳本名。之前的版本該變量不可用。 |
$_SERVER['GATEWAY_INTERFACE'] | 服務器使用的 CGI 規(guī)范的版本;例如,"CGI/1.1"。 |
$_SERVER['SERVER_ADDR'] | 當前運行腳本所在的服務器的 IP 地址。 |
$_SERVER['SERVER_NAME'] | 當前運行腳本所在的服務器的主機名。如果腳本運行于虛擬主機中,該名稱是由那個虛擬主機所設置的值決定。(如: www.manongjc.com) |
$_SERVER['SERVER_SOFTWARE'] | 服務器標識字符串,在響應請求時的頭信息中給出。 (如:Apache/2.2.24) |
$_SERVER['SERVER_PROTOCOL'] | 請求頁面時通信協(xié)議的名稱和版本。例如,"HTTP/1.0"。 |
$_SERVER['REQUEST_METHOD'] | 訪問頁面使用的請求方法;例如,"GET", "HEAD","POST","PUT"。 |
$_SERVER['REQUEST_TIME'] | 請求開始時的時間戳。從 PHP 5.1.0 起可用。 (如:1377687496) |
$_SERVER['QUERY_STRING'] | query string(查詢字符串),如果有的話,通過它進行頁面訪問。 |
$_SERVER['HTTP_ACCEPT'] | 當前請求頭中 Accept: 項的內(nèi)容,如果存在的話。 |
$_SERVER['HTTP_ACCEPT_CHARSET'] | 當前請求頭中 Accept-Charset: 項的內(nèi)容,如果存在的話。例如:"iso-8859-1,*,utf-8"。 |
$_SERVER['HTTP_HOST'] | 當前請求頭中 Host: 項的內(nèi)容,如果存在的話。 |
$_SERVER['HTTP_REFERER'] | 引導用戶代理到當前頁的前一頁的地址(如果存在)。由 user agent 設置決定。并不是所有的用戶代理都會設置該項,有的還提供了修改 HTTP_REFERER 的功能。簡言之,該值并不可信。) |
$_SERVER['HTTPS'] | 如果腳本是通過 HTTPS 協(xié)議被訪問,則被設為一個非空的值。 |
$_SERVER['REMOTE_ADDR'] | 瀏覽當前頁面的用戶的 IP 地址。 |
$_SERVER['REMOTE_HOST'] | 瀏覽當前頁面的用戶的主機名。DNS 反向解析不依賴于用戶的 REMOTE_ADDR。 |
$_SERVER['REMOTE_PORT'] | 用戶機器上連接到 Web 服務器所使用的端口號。 |
$_SERVER['SCRIPT_FILENAME'] | 當前執(zhí)行腳本的絕對路徑。 |
$_SERVER['SERVER_ADMIN'] | 該值指明了 Apache 服務器配置文件中的 SERVER_ADMIN 參數(shù)。如果腳本運行在一個虛擬主機上,則該值是那個虛擬主機的值。(如:someone@manongjc.com) |
$_SERVER['SERVER_PORT'] | Web 服務器使用的端口。默認值為 "80"。如果使用 SSL 安全連接,則這個值為用戶設置的 HTTP 端口。 |
$_SERVER['SERVER_SIGNATURE'] | 包含了服務器版本和虛擬主機名的字符串。 |
$_SERVER['PATH_TRANSLATED'] | 當前腳本所在文件系統(tǒng)(非文檔根目錄)的基本路徑。這是在服務器進行虛擬到真實路徑的映像后的結(jié)果。 |
$_SERVER['SCRIPT_NAME'] | 包含當前腳本的路徑。這在頁面需要指向自己時非常有用。__FILE__ 常量包含當前腳本(例如包含文件)的完整路徑和文件名。 |
$_SERVER['SCRIPT_URI'] | URI 用來指定要訪問的頁面。例如 "/index.html"。 |
PHP $_REQUEST
PHP $_REQUEST 用于收集HTML表單提交的數(shù)據(jù)。
以下實例顯示了一個輸入字段(input)及提交按鈕(submit)的表單(form)。 當用戶通過點擊 "Submit" 按鈕提交表單數(shù)據(jù)時, 表單數(shù)據(jù)將發(fā)送至

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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

The difference between C++ local variables and global variables: Visibility: Local variables are limited to the defining function, while global variables are visible throughout the program. Memory allocation: local variables are allocated on the stack, while global variables are allocated in the global data area. Scope: Local variables are within a function, while global variables are throughout the program. Initialization: Local variables are initialized when a function is called, while global variables are initialized when the program starts. Recreation: Local variables are recreated on every function call, while global variables are created only when the program starts.

The Chinese meaning of request is "request". It is a global variable in PHP and is an array containing "$_POST", "$_GET" and "$_COOKIE". The "$_REQUEST" variable can obtain data and COOKIE information submitted by POST or GET.

The go language does not have static global variables. It uses a more flexible way to handle the need for global variables. Global variables are usually declared at the package level, that is, variables declared outside the function. These global variables are throughout the package. are visible and can be used in any function in the package.

As JavaScript becomes more popular, more and more websites and applications rely on JavaScript. However, the use of global variables in JavaScript can have security issues. In this article, I will introduce how to implement global variable safety in JavaScript. The best way to avoid using global variables is to avoid using global variables. In JavaScript, all variables are global by default unless they are declared within a function. Therefore, local variables should be used whenever possible

Golang is a strongly typed programming language with features such as efficiency, simplicity, and concurrency, so it is gradually favored by more and more developers. In the development of Golang, the global variables and local variables of functions often involve data competition issues. This article will analyze the data competition problem of global variables and local variables in Golang functions from the perspective of actual coding. 1. Data competition for global variables Golang global variables can be accessed in all functions, so if rigorous design and coding are not carried out

We will see how C and C++ behave differently when redeclaring a global variable without initialization, redeclaring a global variable with initialization, and redeclaring a global variable and initializing it twice. Additionally, we will repeat the above combination using local variables. 1.A) C program: Re-declare global variables without initialization #include<stdio.h>intvar;intvar;intmain(){ printf("Var=%d",var); return0;} output Var=0B) C++ program:

Yes, Go functions in Goroutine have direct access to global variables by default. Reason: Goroutine inherits the memory space of the Goroutine that created it, including access to global variables.

PHP global variables include: 1. $_SERVER, the super global variable of the server and execution environment information on which the current script is running; 2. $_GET, an associative array of variables passed to the current script through the GET method; 3. $_POST, through the POST method An associative array of variables passed to the current script; 4. $_SESSION, which stores user-related information in the current session; 5. $_COOKIE, an associative array of variables passed to the current script through HTTP Cookies; 6. $_FILES, etc.
