How to get PHP super global variables (organized and shared)
In the previous article, I brought you "Understand PHP anonymous functions in five minutes (detailed examples)". This article introduces the relevant knowledge of anonymous functions in PHP in detail. This article will take a look at the issues related to super global variables that can be referenced inside functions. I hope it will be helpful to everyone!
PHP super global variables
Global variables defined outside the function cannot be referenced inside the function, but Sometimes you need to use these global variables within a function. In this case, you need to use super global variables. Super global variables can be referenced inside the function.
Several super global variables are predefined in PHP, which means that they can be referenced in the entire scope of a script. Without special instructions, super global variables can be used in functions and classes.
PHP super global variable:
$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$ _FILES
$_ENV
$_COOKIE
- ## $_SESSION
PHP $GLOBALS
$GLOBALS is a A predefined superglobal array that contains all variables available in the global scope. The name of the variable is the key of the array. $GLOBALS is accessible from the entire scope of a PHP script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
global
global that is very similar to $GLOBALS, which also allows us to Use global variables defined outside the function inside the function.
1 |
|
- The global keyword cannot be used outside the function, but can only be used inside the function; ## The #global keyword can only be used to refer to global variables outside the function, and cannot be directly assigned when referencing. The assignment and declaration statements need to be written separately;
- Destroy a global variable inside the function using the global key When modifying variables with words, variables outside the function are not affected.
- The example is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
In the above example, three variables are defined, but the global keyword only modifies two variables in the function, What impact will the output have?
Output result:
It can be seen that the result only outputs variables a and b, because the global keyword is only modified within the function There are two, so the variable c is not used successfully.
Through two examples, we can see that compared with global, $GLOBALS has the following differences:
- global $ refers to the variable with the same name outside the function References are two variables that do not affect each other, while $GLOBALS[] refers to the external variable of the function itself, which is a variable.
- $GLOBALS is not limited to being used inside a function and can be used anywhere in the program.
PHP $_SERVER##PHP $_SERVER is an array to be precise, $_SERVER contains Header information, path, script location and other information. The items in this array are created by the web server. Servers may ignore some, and not all items may be available on every server.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
Share with everyone , More important elements in the $_SERVER variable:
- $_SERVER['PHP_SELF']
- ---The file name of the currently executing script, related to document root .
- ---The version of the CGI specification used by the server.
$_SERVER['SERVER_ADDR']
---The IP address of the server where the script is currently running.$_SERVER['SERVER_NAME']
---The host name of the server where the script is currently running.$_SERVER['SERVER_SOFTWARE']
---Server identification string, given in the header information when responding to the request.$_SERVER['SERVER_PROTOCOL']
---The name and version of the communication protocol when requesting the page.$_SERVER['REQUEST_METHOD']
---The request method used to access the page.$_SERVER['REQUEST_TIME']
---The timestamp when the request started. Available since PHP 5.1.0.$_SERVER['QUERY_STRING']
---query string (query string), if any, use it to access the page.$_SERVER['HTTP_ACCEPT']
---The content of the Accept: item in the current request header, if it exists.$_SERVER['HTTP_ACCEPT_CHARSET']
---The content of the Accept-Charset: item in the current request header, if it exists.$_SERVER['HTTP_HOST']
---The content of the Host: item in the current request header, if it exists.$_SERVER['HTTP_REFERER']
---Direct the user agent to the address of the previous page of the current page (if it exists).$_SERVER['HTTPS']
---If the script is accessed through the HTTPS protocol, it is set to a non-empty value.$_SERVER['REMOTE_ADDR']
---The IP address of the user browsing the current page.$_SERVER['REMOTE_HOST']
---The host name of the user browsing the current page. DNS reverse resolution does not depend on the user's REMOTE_ADDR.$_SERVER['REMOTE_PORT']
---The port number used on the user's machine to connect to the Web server.$_SERVER['SCRIPT_FILENAME']
---The absolute path of the currently executing script.$_SERVER['SERVER_ADMIN']
---This value specifies the SERVER_ADMIN parameter in the Apache server configuration file. If the script is running on a virtual host, this value is that of that virtual host.$_SERVER['SERVER_PORT']
---The port used by the Web server. The default value is "80". If using SSL secure connection, this value is the HTTP port set by the user.$_SERVER['SERVER_SIGNATURE']
---A string containing the server version and virtual host name.$_SERVER['PATH_TRANSLATED']
---The base path of the file system (not the document root directory) where the current script is located. This is the result after the server has been imaged from a virtual to real path.$_SERVER['SCRIPT_NAME']
---Contains the path of the current script. This is useful when the page needs to point to itself. The __FILE__ constant contains the full path and file name of the current script (such as an include file).$_SERVER['SCRIPT_URI']
---URI is used to specify the page to be accessed. For example "/index.html".
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to get PHP super global variables (organized and shared). For more information, please follow other related articles on the PHP Chinese website!

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





PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

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,

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

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

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.

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.
