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.
<?php //定义两个全局变量,函数内部不可以访问 $a = 75; $b = 25; //定义函数 function addition() { //将全局变量变为超级全局变量,这样在函数内部就可以正常访问了 $GLOBALS['c'] = $GLOBALS['a'] + $GLOBALS['b']; } //调用函数 addition(); //输出函数内部定义的全局变量 echo $c; ?>Output result:
global
global that is very similar to $GLOBALS, which also allows us to Use global variables defined outside the function inside the function.
global 变量1, 变量2, ...global keyword can be followed by multiple variables as parameters, and multiple variables are separated by "," (comma). At the same time, you should pay attention to some key points when using global:
- 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:
<?php $a = 1; $b = 2; $c = 3; function demo(){ global $a, $b; echo "变量 a:".$a; echo "<br>变量 b:".$b; echo "<br>变量 c:".$c; } demo(); ?>
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.
<?php //输出当前脚步的文件名 echo "<h3 id="输出当前脚步的文件名">输出当前脚步的文件名</h3>"; echo $_SERVER['PHP_SELF']; echo "<hr/>"; //当前脚步所在服务器的主机名 echo "<h3 id="当前脚步所在服务器的主机名">当前脚步所在服务器的主机名</h3>"; echo $_SERVER['SERVER_NAME']; echo "<hr/>"; //当前请求头中 Host echo "<h3 id="当前请求头中-nbsp-Host">当前请求头中 Host</h3>"; echo $_SERVER['HTTP_HOST']; echo "<hr/>"; //引导用户代理到当前页的前一页的地址(如果存在) echo "<h3 id="引导用户代理到当前页的前一页的地址-如果存在">引导用户代理到当前页的前一页的地址(如果存在)</h3>"; echo $_SERVER['HTTP_REFERER']; echo "<hr/>"; //用来检查浏览页面的访问者在用什么操作系统 echo "<h3 id="用来检查浏览页面的访问者在用什么操作系统">用来检查浏览页面的访问者在用什么操作系统</h3>"; echo $_SERVER['HTTP_USER_AGENT']; echo "<hr/>"; //包含当前脚本的路径 echo "<h3 id="包含当前脚本的路径">包含当前脚本的路径</h3>"; echo $_SERVER['SCRIPT_NAME']; ?>Output results
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!

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

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 makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

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 still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool