This article brings you relevant knowledge about PHP, which mainly introduces the relevant content about super global variables. Super global variables are a special type of variables, which are built-in and predefined. Accessed from any scope, there is no need to execute any special code segments. Let’s take a look at it. I hope it will be helpful to everyone.
Understanding PHP Super Globals
Super global variables are a special type of variable because they can be accessed from any scope. Can be accessed from any file, class, or even function without executing any special code segment.
Superglobal variables are built-in and predefined. Programmers can use them through PHP libraries. Please note that not all built-in predefined variables in the class library are superglobal variables.
Prerequisites
To understand the content of this article, readers should have the following conditions.
- Have a basic understanding of PHP variable declaration technology.
- Have a preliminary understanding of PHP.
Introduction to PHP super global variables
Super global variables were introduced in PHP 4.1.0 and have been an important part of PHP ever since. There are about 9 superglobal variables in PHP, sometimes called automatic globals
. They are described below.
$GLOBALS
$_SERVER
$_GET
$_POST
$_REQUEST
$_SESSION
$_COOKIE
$_FILE
$_ENV
Let us discuss these super global variables in the following sections .
$GLOBALS
$GLOBAL The following is an example of using the super global variable :)<pre class="brush:php;toolbar:false"> nbsp;html>
<title>GLOBAL example</title>
<?php //php Script
// Varriable declaration
$a = 5;
$b = 6;
function multiplication(){
$GLOBALS[&#39;c&#39;] = $GLOBALS[&#39;a&#39;]* $GLOBALS[&#39;b&#39;];
}
multiplication();
echo $c;
?>
</pre>
In the above example, the variable $c
is inside the function and It is accessible from the outside because it is in the
array.
$_SERVER
is a super global variable used to save the information header, path and location of the PHP script. Variables have several elements that are saved. They include
$_SERVER['PHP_SELF'] - which returns the filename of the currently executing script. $_SERVER['SERVER_NAME'] - This returns the name of the server hosting the website.
$_SERVER['HTTP_HOST'] - This will return the host header of the current request.
$_SERVER['SCRIPT_NAME'] - This returns the path to the current script.
-
Below is a sample code showing how to use the above elements.
nbsp;html> <title> $_SERVER example</title> <?php // PHP script echo $_SERVER['PHP_SELF']; echo "<br>"; echo $_SERVER['SERVER_NAME']; echo "<br>"; echo $_SERVER['HTTP_HOST']; echo "<br>"; echo $_SERVER['SCRIPT_NAME']; ?>
The output of the above code will include. -
- 一个文件名。
- 主机服务器的名称。
- 主机当前请求的标题。
- 当前脚本的路径。
$_GET
$_GET
变量是一个PHP超全局变量,用于收集HTML表单提交后的数据。HTML表单的结构是这样的:$_GET
作为一个方法。$_GET
也可以用来检索在uniform resource locator
中发送的数据。下面是一个例子,说明如何在HTML表单中实现
$_GET
变量。nbsp;html> <title>$_GET example</title> <!-- html form -->
当用户点击
Submit
按钮时,表单中的信息会用GET
方法发送,并显示在URL
。然而,每次最多只能发送2048
字符。$_POST
就像
$_GET
变量一样,$_POST
收集来自HTML表单的值。使用这种方法发送的信息不会显示在URL中。一次可以发送的字符数也没有限制。下面是一个例子。
nbsp;html> <title>$_POST example</title> <!-- html form -->
为什么POST变量优于GET?
尽管
POST
和GET
方法实现了相同的功能,但由于以下原因,POST
更受青睐。POST方法对可以发送的数据大小没有限制。
POST方法可以同时发送ASCII和二进制数据。
POST方法不会在URL上显示正在发送的信息,因此可以防止建立书签。
POST方法使用一个
HTTP header
来发送数据。这促进了数据安全。
$_REQUEST
$_REQUEST
变量是一个PHP超全局,用于在提交表单后收集数据。它包含了$_GET
,$_POST
,甚至默认的$_COOKIE
的内容。各个字段的数据可以由PHP使用$_REQUEST
变量来收集。下面的例子显示了如何使用
$_REQUEST
这个变量。nbsp;html> <title>$_REQUEST example</title>
上述代码的输出将是表单中已提交的
name
。如果没有提交名字,它将打印一个信息Empty name
。$_SESSION
$_SESSION
变量是一个PHP的超级全局,它可以在用户每次打开网站时存储和利用有关网站用户的信息,直到网站关闭。每次用户访问网站时,都会启动一个会话。下面的函数被用来在PHP代码中启动一个会话。
session_start()
会话开始后,需要使用
$_SESSION
变量进行设置。当用户离开一个网站时,会话被自动销毁。这是在用户不知情的情况下使用下面的PHP函数完成的。
session_destroy()
下面的例子演示了
$_SESSION
的使用。php session_start(); ?> nbsp;html> <title>$_SESSION demonstration code</title> <?php //Set session varriables $_SESSION["name"]="Mackrine"; $_SESSION["favcolor"]="Blue"; echo "session varriables are set"; ?>
$_COOKIE
Cookie是一个小文件,由服务器存储在用户的计算机中。它可以识别用户。每当向服务器发出请求时。通常会在请求的同时发送一个cookie。PHP 使用
setcookie()
函数创建 cookie。setcookie(cookie_name,cookie_value, expiry, path, domain,secure,httponly)
该语法有许多参数。然而,只有
name
参数是必需的。在创建之后,可以使用超全局
$_COOKIE
变量来检索cookie。下面的代码显示了如何创建和检索一个cookie。<?php $cookie_name = "uname"; $cookie_value = "Mackrine"; //setting cookie setcookie($cookie_name, $cookie_value, time()+(86400*30),"/"); ?> nbsp;html> <?php if(isset($_COOKIE[$cookie_name])) { echo "Cookie name:" .$cookie_name; echo "<br>"; echo "Cookie value:" .$cookie_value; } else { echo $cookie_name. " is not set!"; } ?>
只有在过期的情况下,才可以使用
setcookie()
函数删除cookie。$_FILES
$_FILES
是一个变量,包含使用HTTPPOST方法上传的项目。 数组包含几个元素,如下所述。$_FILES
$_FILES['file']['name'] - 这通常是要上传的文件的原始名称。
$_FILES['file']['type'] - 这是指被上传文件的类型。
$_FILES['file']['size'] - 以字节为单位的文件大小。
$_FILES['file']['tmp_name'] - 它指的是在服务器上上传的存储文件的临时文件名。
$_FILE['file']['error']- 文件上传的相关错误代码。
总结
超全局变量是PHP语言的核心。在PHP编程中需要这些变量来制作高功能的程序。因此,你可以利用这些信息来制作高质量的应用程序。
推荐学习:《PHP视频教程》
The above is the detailed content of Simple understanding of PHP super global variables. For more information, please follow other related articles on the PHP Chinese website!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft