Home > Article > Backend Development > Simple understanding of PHP super global variables
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.
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.
To understand the content of this article, readers should have the following conditions.
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 .
$GLOBAL The following is an example of using the super global variable :)<pre class="brush:php;toolbar:false"> <!doctype html>
<html>
<head>
<title>GLOBAL example</title>
</head>
<body>
<?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;
?>
</body>
</html></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.
<!doctype html> <html> <head> <title> $_SERVER example</title> </head> <body> <?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']; ?> </body> </html>
$_GET
变量是一个PHP超全局变量,用于收集HTML表单提交后的数据。HTML表单的结构是这样的:$_GET
作为一个方法。$_GET
也可以用来检索在uniform resource locator
中发送的数据。
下面是一个例子,说明如何在HTML表单中实现$_GET
变量。
<!doctype html> <html> <head> <title>$_GET example</title> </head> <body> <!-- html form --> <form action="" method="GET"> <label>Name</label> <input type="text" name="Name"> <label>Email</label> <input type="text" name="Email"> <button>Submit</button> </form> </body> </html>
当用户点击Submit
按钮时,表单中的信息会用GET
方法发送,并显示在URL
。然而,每次最多只能发送2048
字符。
就像$_GET
变量一样,$_POST
收集来自HTML表单的值。使用这种方法发送的信息不会显示在URL中。一次可以发送的字符数也没有限制。
下面是一个例子。
<!doctype html> <html> <head> <title>$_POST example</title> </head> <body> <!-- html form --> <form action="" method="POST"> <label>Name</label> <input type="text" name="Name"> <label>Email</label> <input type="text" name="Email"> <button>Submit</button> </form> </body> </html>
尽管POST
和GET
方法实现了相同的功能,但由于以下原因,POST
更受青睐。
POST方法对可以发送的数据大小没有限制。
POST方法可以同时发送ASCII和二进制数据。
POST方法不会在URL上显示正在发送的信息,因此可以防止建立书签。
POST方法使用一个HTTP header
来发送数据。这促进了数据安全。
$_REQUEST
变量是一个PHP超全局,用于在提交表单后收集数据。它包含了$_GET
,$_POST
,甚至默认的$_COOKIE
的内容。各个字段的数据可以由PHP使用$_REQUEST
变量来收集。
下面的例子显示了如何使用$_REQUEST
这个变量。
<!doctype html> <html> <head> <title>$_REQUEST example</title> </head> <body> <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST"> <label>Name</label> <input type="text" myname="Name"> <button>Submit</button> </form> <?php if($_SERVER["REQUEST_METHOD"]=="POST"){ $name = $_REQUEST['myname']; if(!empty($myname)) { echo $myname; }else{ echo "Empty name"; } } ?> </body> </html>
上述代码的输出将是表单中已提交的name
。如果没有提交名字,它将打印一个信息Empty name
。
$_SESSION
变量是一个PHP的超级全局,它可以在用户每次打开网站时存储和利用有关网站用户的信息,直到网站关闭。
每次用户访问网站时,都会启动一个会话。下面的函数被用来在PHP代码中启动一个会话。
session_start()
会话开始后,需要使用$_SESSION
变量进行设置。
当用户离开一个网站时,会话被自动销毁。这是在用户不知情的情况下使用下面的PHP函数完成的。
session_destroy()
下面的例子演示了$_SESSION
的使用。
<? php session_start(); ?> <!doctype html> <html> <head> <title>$_SESSION demonstration code</title> </head> <body> <?php //Set session varriables $_SESSION["name"]="Mackrine"; $_SESSION["favcolor"]="Blue"; echo "session varriables are set"; ?> </body> </html>
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),"/"); ?> <!doctype html> <html> <body> <?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!"; } ?> </body> </html>
只有在过期的情况下,才可以使用setcookie()
函数删除cookie。
$_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!