當(dāng)用戶點(diǎn)擊鏈接 \"Test $GET\", 參數(shù) \"subject\" 和 \"web\" 將發(fā)送至\"test_get.php\",你可以在 \"test_get.php\" 文件中使用 $_GET 變量來(lái)獲取這些數(shù)據(jù)。<\/p>\n
\n
\n\n\n\n<?php \necho \"Study \" . $_GET['subject'] . \" at \" . $_GET['web'];\n?>\n\n<\/body>\n<\/html><\/pre>\n<\/div>\n提示:<\/strong>?你如果想學(xué)習(xí)更多關(guān)于 $_POST 和 $_GET 的知識(shí),請(qǐng)?jiān)L問我們的?PHP 表單?章節(jié)。<\/p>\n以上就是小編為大家?guī)?lái)的全面了解PHP中的全局變量全部?jī)?nèi)容了,希望大家多多支持腳本之家~<\/p>\n\n"}
Home
Backend Development
PHP Tutorial
Comprehensive understanding of global variables in PHP_php examples
Comprehensive understanding of global variables in PHP_php examples
Jul 06, 2016 pm 01:32 PM
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:
<?php
$x = 75;
$y = 25;
function addition()
{
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}
addition();
echo $z;
?>
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:
<?php
echo $_SERVER['PHP_SELF'];
echo "<br>";
echo $_SERVER['SERVER_NAME'];
echo "<br>";
echo $_SERVER['HTTP_HOST'];
echo "<br>";
echo $_SERVER['HTTP_REFERER'];
echo "<br>";
echo $_SERVER['HTTP_USER_AGENT'];
echo "<br>";
echo $_SERVER['SCRIPT_NAME'];
?>
The following table lists the important elements in all $_SERVER variables:
元素/代碼 |
描述 |
$_SERVER['PHP_SELF'] |
當(dāng)前執(zhí)行腳本的文件名,與 document root 有關(guān)。例如,在地址為 http://example.com/test.php/foo.bar 的腳本中使用 $_SERVER['PHP_SELF'] 將得到 /test.php/foo.bar。__FILE__ 常量包含當(dāng)前(例如包含)文件的完整路徑和文件名。 從 PHP 4.3.0 版本開始,如果 PHP 以命令行模式運(yùn)行,這個(gè)變量將包含腳本名。之前的版本該變量不可用。 |
$_SERVER['GATEWAY_INTERFACE'] |
服務(wù)器使用的 CGI 規(guī)范的版本;例如,"CGI/1.1"。 |
$_SERVER['SERVER_ADDR'] |
當(dāng)前運(yùn)行腳本所在的服務(wù)器的 IP 地址。 |
$_SERVER['SERVER_NAME'] |
當(dāng)前運(yùn)行腳本所在的服務(wù)器的主機(jī)名。如果腳本運(yùn)行于虛擬主機(jī)中,該名稱是由那個(gè)虛擬主機(jī)所設(shè)置的值決定。(如: www.manongjc.com) |
$_SERVER['SERVER_SOFTWARE'] |
服務(wù)器標(biāo)識(shí)字符串,在響應(yīng)請(qǐng)求時(shí)的頭信息中給出。 (如:Apache/2.2.24) |
$_SERVER['SERVER_PROTOCOL'] |
請(qǐng)求頁(yè)面時(shí)通信協(xié)議的名稱和版本。例如,"HTTP/1.0"。 |
$_SERVER['REQUEST_METHOD'] |
訪問頁(yè)面使用的請(qǐng)求方法;例如,"GET", "HEAD","POST","PUT"。 |
$_SERVER['REQUEST_TIME'] |
請(qǐng)求開始時(shí)的時(shí)間戳。從 PHP 5.1.0 起可用。 (如:1377687496) |
$_SERVER['QUERY_STRING'] |
query string(查詢字符串),如果有的話,通過它進(jìn)行頁(yè)面訪問。 |
$_SERVER['HTTP_ACCEPT'] |
當(dāng)前請(qǐng)求頭中 Accept: 項(xiàng)的內(nèi)容,如果存在的話。 |
$_SERVER['HTTP_ACCEPT_CHARSET'] |
當(dāng)前請(qǐng)求頭中 Accept-Charset: 項(xiàng)的內(nèi)容,如果存在的話。例如:"iso-8859-1,*,utf-8"。 |
$_SERVER['HTTP_HOST'] |
當(dāng)前請(qǐng)求頭中 Host: 項(xiàng)的內(nèi)容,如果存在的話。 |
$_SERVER['HTTP_REFERER'] |
引導(dǎo)用戶代理到當(dāng)前頁(yè)的前一頁(yè)的地址(如果存在)。由 user agent 設(shè)置決定。并不是所有的用戶代理都會(huì)設(shè)置該項(xiàng),有的還提供了修改 HTTP_REFERER 的功能。簡(jiǎn)言之,該值并不可信。) |
$_SERVER['HTTPS'] |
如果腳本是通過 HTTPS 協(xié)議被訪問,則被設(shè)為一個(gè)非空的值。 |
$_SERVER['REMOTE_ADDR'] |
瀏覽當(dāng)前頁(yè)面的用戶的 IP 地址。 |
$_SERVER['REMOTE_HOST'] |
瀏覽當(dāng)前頁(yè)面的用戶的主機(jī)名。DNS 反向解析不依賴于用戶的 REMOTE_ADDR。 |
$_SERVER['REMOTE_PORT'] |
用戶機(jī)器上連接到 Web 服務(wù)器所使用的端口號(hào)。 |
$_SERVER['SCRIPT_FILENAME'] |
當(dāng)前執(zhí)行腳本的絕對(duì)路徑。 |
$_SERVER['SERVER_ADMIN'] |
該值指明了 Apache 服務(wù)器配置文件中的 SERVER_ADMIN 參數(shù)。如果腳本運(yùn)行在一個(gè)虛擬主機(jī)上,則該值是那個(gè)虛擬主機(jī)的值。(如:someone@manongjc.com) |
$_SERVER['SERVER_PORT'] |
Web 服務(wù)器使用的端口。默認(rèn)值為 "80"。如果使用 SSL 安全連接,則這個(gè)值為用戶設(shè)置的 HTTP 端口。 |
$_SERVER['SERVER_SIGNATURE'] |
包含了服務(wù)器版本和虛擬主機(jī)名的字符串。 |
$_SERVER['PATH_TRANSLATED'] |
當(dāng)前腳本所在文件系統(tǒng)(非文檔根目錄)的基本路徑。這是在服務(wù)器進(jìn)行虛擬到真實(shí)路徑的映像后的結(jié)果。 |
$_SERVER['SCRIPT_NAME'] |
包含當(dāng)前腳本的路徑。這在頁(yè)面需要指向自己時(shí)非常有用。__FILE__ 常量包含當(dāng)前腳本(例如包含文件)的完整路徑和文件名。 |
$_SERVER['SCRIPT_URI'] |
URI 用來(lái)指定要訪問的頁(yè)面。例如 "/index.html"。 |
PHP $_REQUEST
PHP $_REQUEST 用于收集HTML表單提交的數(shù)據(jù)。
以下實(shí)例顯示了一個(gè)輸入字段(input)及提交按鈕(submit)的表單(form)。 當(dāng)用戶通過點(diǎn)擊 "Submit" 按鈕提交表單數(shù)據(jù)時(shí), 表單數(shù)據(jù)將發(fā)送至
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
How to get the current session ID in PHP?
Jul 13, 2025 am 03:02 AM
The method to get the current session ID in PHP is to use the session_id() function, but you must call session_start() to successfully obtain it. 1. Call session_start() to start the session; 2. Use session_id() to read the session ID and output a string similar to abc123def456ghi789; 3. If the return is empty, check whether session_start() is missing, whether the user accesses for the first time, or whether the session is destroyed; 4. The session ID can be used for logging, security verification and cross-request communication, but security needs to be paid attention to. Make sure that the session is correctly enabled and the ID can be obtained successfully.
PHP get substring from a string
Jul 13, 2025 am 02:59 AM
To extract substrings from PHP strings, you can use the substr() function, which is syntax substr(string$string,int$start,?int$length=null), and if the length is not specified, it will be intercepted to the end; when processing multi-byte characters such as Chinese, you should use the mb_substr() function to avoid garbled code; if you need to intercept the string according to a specific separator, you can use exploit() or combine strpos() and substr() to implement it, such as extracting file name extensions or domain names.
How do you perform unit testing for php code?
Jul 13, 2025 am 02:54 AM
UnittestinginPHPinvolvesverifyingindividualcodeunitslikefunctionsormethodstocatchbugsearlyandensurereliablerefactoring.1)SetupPHPUnitviaComposer,createatestdirectory,andconfigureautoloadandphpunit.xml.2)Writetestcasesfollowingthearrange-act-assertpat
How to split a string into an array in PHP
Jul 13, 2025 am 02:59 AM
In PHP, the most common method is to split the string into an array using the exploit() function. This function divides the string into multiple parts through the specified delimiter and returns an array. The syntax is exploit(separator, string, limit), where separator is the separator, string is the original string, and limit is an optional parameter to control the maximum number of segments. For example $str="apple,banana,orange";$arr=explode(",",$str); The result is ["apple","bana
JavaScript Data Types: Primitive vs Reference
Jul 13, 2025 am 02:43 AM
JavaScript data types are divided into primitive types and reference types. Primitive types include string, number, boolean, null, undefined, and symbol. The values are immutable and copies are copied when assigning values, so they do not affect each other; reference types such as objects, arrays and functions store memory addresses, and variables pointing to the same object will affect each other. Typeof and instanceof can be used to determine types, but pay attention to the historical issues of typeofnull. Understanding these two types of differences can help write more stable and reliable code.
Using std::chrono in C
Jul 15, 2025 am 01:30 AM
std::chrono is used in C to process time, including obtaining the current time, measuring execution time, operation time point and duration, and formatting analysis time. 1. Use std::chrono::system_clock::now() to obtain the current time, which can be converted into a readable string, but the system clock may not be monotonous; 2. Use std::chrono::steady_clock to measure the execution time to ensure monotony, and convert it into milliseconds, seconds and other units through duration_cast; 3. Time point (time_point) and duration (duration) can be interoperable, but attention should be paid to unit compatibility and clock epoch (epoch)
How to pass a session variable to another page in PHP?
Jul 13, 2025 am 02:39 AM
In PHP, to pass a session variable to another page, the key is to start the session correctly and use the same $_SESSION key name. 1. Before using session variables for each page, it must be called session_start() and placed in the front of the script; 2. Set session variables such as $_SESSION['username']='JohnDoe' on the first page; 3. After calling session_start() on another page, access the variables through the same key name; 4. Make sure that session_start() is called on each page, avoid outputting content in advance, and check that the session storage path on the server is writable; 5. Use ses
PHP header location not working after include
Jul 13, 2025 am 02:08 AM
When encountering the problem that header('Location:...') does not work, the common reasons and solutions are as follows: 1. There is output in advance, causing the header to fail. The solution is to ensure that there is no output before the jump, including spaces, HTML or echo; 2. There is excess output or UTF-8 BOM characters in the include or require file. The file encoding should be checked and saved as "UTF-8 BOM-free"; 3. It is recommended to use ob_start() to turn on the output buffer before the jump, and cooperate with ob_end_flush() to delay the output; 4. After the jump, be sure to add exit to prevent subsequent code execution; 5. Make sure that the header() function call is before all outputs.
See all articles