PHP Superglobals

PHP Superglobals (PHP超全局变量)

PHP Superglobals: Understanding the Basics

PHP Superglobals: Understanding the Basics

PHP is a widely used programming language, especially for web development. It offers several variables that are available in all scopes, called “superglobals”. In this article, we will cover the basics of PHP superglobals and how to use them effectively in your web development projects. (PHP是一种广泛使用的编程语言,特别是用于Web开发。它提供了几个在所有范围内都可用的变量,称为“超全局”。在本文中,我们将介绍PHP超全局的基础知识以及如何在Web开发项目中有效地使用它们。)

What are PHP Superglobals?

PHP superglobals are predefined variables in PHP that are available in all scopes. This means that you can access these variables from anywhere in your code, regardless of the function or class you are in. There are nine superglobals in PHP, but the most commonly used ones are $_GET, $_POST, $_COOKIE, $_SESSION, and $_SERVER. (PHP超全局变量是PHP中的预定义变量,可在所有作用域中使用。这意味着您可以从代码中的任何位置访问这些变量,无论您所在的函数或类如何。PHP中有九个超全局变量,但最常用的是$_GET、$_POST、$_COOKIE、$_SESSION和$_SERVER。)

$_GET

The $_GET superglobal is used to collect data from URL parameters. For example, if you have a URL like “example.com/?name=John”, the value of “John” can be retrieved using $_GET[’name’]. This is often used for form submissions, where the user’s input is sent via the URL. ($_GET超全局用于从URL参数收集数据。例如,如果您有一个像“example.com/?name=John”这样的URL ,则可以使用$_GET [’name’]检索“John”的值。这通常用于表单提交,其中用户的输入通过URL发送。)

$_POST

The $_POST superglobal is used to collect data from form submissions. Unlike $_GET, the data is sent via the HTTP request body, not the URL. This makes it more secure, as the data is not visible in the URL. To access the data, you would use $_POST[‘field_name’]. ($_POST超级全局用于从表单提交中收集数据。与$_GET不同,数据是通过HTTP请求正文而不是URL发送的。这使得它更安全,因为数据在URL中不可见。要访问数据,您将使用$_POST [‘FIELD_NAME’]。)

The $_COOKIE superglobal is used to collect data from HTTP cookies. A cookie is a small text file that is stored on the user’s device, and it can be used to track the user’s activity on your website. To access a cookie, you would use $_COOKIE[‘cookie_name’]. ($_COOKIE超全局用于从HTTP Cookie收集数据。Cookie是存储在用户设备上的小型文本文件,可用于跟踪用户在您网站上的活动。要访问Cookie ,请使用$_COOKIE [‘COOKIE_NAME’]。)

$_SESSION

The $_SESSION superglobal is used to store data across multiple pages on your website. Unlike cookies, the data is stored on the server, not on the user’s device. To access a session variable, you would use $_SESSION[‘session_name’]. ($_SESSION超全局用于存储您网站上多个页面的数据。与Cookie不同,数据存储在服务器上,而不是用户的设备上。要访问会话变量,您将使用$_SESSION [‘SESSION_NAME’]。)

$_SERVER

The $_SERVER superglobal is used to collect information about the server and the current request. For example, you can use $_SERVER[‘HTTP_HOST’] to get the host name of the current website, or $_SERVER[‘REQUEST_METHOD’] to get the HTTP method of the current request (e.g. GET or POST). ($_SERVER超全局用于收集有关服务器和当前请求的信息。例如,您可以使用$_SERVER [‘HTTP_HOST’]获取当前网站的主机名,或使用$_SERVER [‘REQUEST_METHOD’]获取当前请求的HTTP方法(例如GET或POST )。)

Conclusion

Conclusion (小结)

In conclusion, PHP superglobals are an essential tool for web development in PHP. By understanding the basics of each superglobal and how to use them, you can easily access and manage data in your web applications. Whether you are working on a small website or a complex web application, PHP superglobals will help you get the job done efficiently. (总之, PHP超全局变量是PHP中Web开发的重要工具。通过了解每个超级全局的基础知识以及如何使用它们,您可以轻松访问和管理Web应用程序中的数据。无论您是在小型网站还是复杂的Web应用程序上工作, PHP超全局变量都将帮助您高效地完成工作。)



请遵守《互联网环境法规》文明发言,欢迎讨论问题
扫码反馈

扫一扫,反馈当前页面

咨询反馈
扫码关注
返回顶部