国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
php global variables, php global variable definition
PHP global variables
PHP $GLOBAL
PHP $_SERVER
PHP $_REQUEST
PHP $_POST
PHP $_GET
Home php教程 php手冊 php global variables, php global variable definition

php global variables, php global variable definition

Jul 06, 2016 pm 02:25 PM
global variables

php 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ā)送至

標簽中 action 屬性中指定的腳本文件。 在這個實例中,我們指定文件來處理表單數(shù)據(jù)。如果你希望其他的PHP文件來處理該數(shù)據(jù),你可以修改該指定的腳本文件名。 然后,我們可以使用超級全局變量 $_REQUEST 來收集表單中的 input 字段數(shù)據(jù):

<span><</span><span>html</span><span>></span>
<span><</span><span>body</span><span>></span>

<span><</span><span>form </span><span>method</span><span>="post"</span><span> action</span><span>="<?php echo $_SERVER['PHP_SELF'];?>"</span><span>></span><span>
Name: </span><span><</span><span>input </span><span>type</span><span>="text"</span><span> name</span><span>="fname"</span><span>></span>
<span><</span><span>input </span><span>type</span><span>="submit"</span><span>></span>
<span></</span><span>form</span><span>></span>

<span><?</span><span>php 
$name = $_REQUEST['fname']; 
echo $name; 
</span><span>?></span>

<span></</span><span>body</span><span>></span>
<span></</span><span>html</span><span>></span>

PHP $_POST

PHP $_POST 被廣泛應用于收集表單數(shù)據(jù),在HTML form標簽的指定該屬性:"method="post"。

以下實例顯示了一個輸入字段(input)及提交按鈕(submit)的表單(form)。 當用戶通過點擊 "Submit" 按鈕提交表單數(shù)據(jù)時, 表單數(shù)據(jù)將發(fā)送至標簽中 action 屬性中指定的腳本文件。 在這個實例中,我們指定文件來處理表單數(shù)據(jù)。如果你希望其他的PHP文件來處理該數(shù)據(jù),你可以修改該指定的腳本文件名。 然后,我們可以使用超級全局變量 $_POST 來收集表單中的 input 字段數(shù)據(jù):

<span><</span><span>html</span><span>></span>
<span><</span><span>body</span><span>></span>

<span><</span><span>form </span><span>method</span><span>="post"</span><span> action</span><span>="<?php echo $_SERVER['PHP_SELF'];?>"</span><span>></span><span>
Name: </span><span><</span><span>input </span><span>type</span><span>="text"</span><span> name</span><span>="fname"</span><span>></span>
<span><</span><span>input </span><span>type</span><span>="submit"</span><span>></span>
<span></</span><span>form</span><span>></span>

<span><?</span><span>php 
$name = $_POST['fname']; 
echo $name; 
</span><span>?></span>

<span></</span><span>body</span><span>></span>
<span></</span><span>html</span><span>></span>

PHP $_GET

PHP $_GET 同樣被廣泛應用于收集表單數(shù)據(jù),在HTML form標簽的指定該屬性:"method="get"。

$_GET 也可以收集URL中發(fā)送的數(shù)據(jù)。

假定我們有一個包含參數(shù)的超鏈接HTML頁面:

<span><</span><span>html</span><span>></span>
<span><</span><span>body</span><span>></span>

<span><</span><span>a </span><span>href</span><span>="test_get.php?subject=PHP&web=manongjc.com"</span><span>></span>Test $GET<span></</span><span>a</span><span>></span>

<span></</span><span>body</span><span>></span>
<span></</span><span>html</span><span>></span>

當用戶點擊鏈接 "Test $GET", 參數(shù) "subject" 和 "web" 將發(fā)送至"test_get.php",你可以在 "test_get.php" 文件中使用 $_GET 變量來獲取這些數(shù)據(jù)。

以下實例顯示了 "test_get.php" 文件的代碼:

<span><</span><span>html</span><span>></span>
<span><</span><span>body</span><span>></span>

<span><?</span><span>php 
echo "Study " . $_GET['subject'] . " at " . $_GET['web'];
</span><span>?></span>

<span></</span><span>body</span><span>></span>
<span></</span><span>html</span><span>></span>

提示:?你如果想學習更多關于 $_POST 和 $_GET 的知識,請訪問我們的?PHP 表單?章節(jié)。

原文地址:http://www.manongjc.com/php/php_globals.html

相關閱讀:

如何將一個表單提交到多個頁面

php 一個頁面處理多個表單

php 表單提交GET與POST實例分享

php 實例之使用表單提交的方法來發(fā)送郵件(用戶反饋)

php 獲取提交表單數(shù)組實例

分享表單提交給本頁的實例

php 過濾表單特殊字符實例

表單提交及php處理表單數(shù)據(jù)的實例

Statement of this Website
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the difference between local variables and global variables of a C++ function? What is the difference between local variables and global variables of a C++ function? Apr 19, 2024 pm 03:42 PM

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.

What does php request mean? What does php request mean? Jul 07, 2021 pm 01:49 PM

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.

Does Go language have static global variables? Does Go language have static global variables? Jul 11, 2023 pm 03:37 PM

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.

Implementing global variable safety in JavaScript Implementing global variable safety in JavaScript Jun 15, 2023 pm 10:33 PM

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

Data competition analysis of global variables and local variables of Golang functions Data competition analysis of global variables and local variables of Golang functions May 21, 2023 am 08:19 AM

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

Redeclaration of global variables in C program Redeclaration of global variables in C program Sep 20, 2023 pm 10:29 PM

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:

Can golang functions directly access global variables in goroutine? Can golang functions directly access global variables in goroutine? May 01, 2024 pm 05:51 PM

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.

What are the global variables in php What are the global variables in php Aug 01, 2023 pm 01:21 PM

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.

See all articles