Home  >  Article  >  Backend Development  >  Let’s talk in depth about superglobal variables in php

Let’s talk in depth about superglobal variables in php

PHPz
PHPzOriginal
2023-04-11 09:15:561069browse

In PHP, a superglobal variable is a special variable that can be accessed anywhere in a script. These variables are automatically set by PHP and take effect globally. PHP provides some super global variables to handle HTTP requests and pass data. In this article, we will delve into how to set these super global variables.

1. $_GET variable

$_GET variable is a super global variable used to process GET requests. This variable is used to get parameter values ​​from the URL when the user submits the form or clicks the URL. Here is a simple example:

<form action="index.php" method="get">
    <input type="text" name="name">
    <input type="submit" value="Submit">
</form>

<?php
    $name = $_GET[&#39;name&#39;];
    echo "Hello $name!";
?>

In the form above, the user can enter their name and submit the name to the index.php file by clicking the submit button. In the index.php file, we use $_GET['name'] to get the name and then display it on the page.

2. $_POST variable

$_POST variable is used to process POST requests. The POST method is typically used to submit sensitive information in a form, such as username and password. The $_POST variable is a set of key-value pairs, where the key is the name of the input field in the form and the value is the value entered by the user. Here is a basic example:

<form action="index.php" method="post">
    <input type="text" name="username">
    <input type="password" name="password">
    <input type="submit" value="Submit">
</form>

<?php
    $username = $_POST[&#39;username&#39;];
    $password = $_POST[&#39;password&#39;];
    echo "Username: $username <br> Password: $password";
?>

In the form above, we collect the username and password and submit them to the index.php file using the POST method. We then use the $_POST variable to get these values ​​and print them out on the screen.

3. $_SERVER variable

The $_SERVER variable contains information about the server and the current script. The following are several commonly used $_SERVER variables:

  • $_SERVER['PHP_SELF']: The file name of the current script.
  • $_SERVER['SERVER_NAME']: The host name of the server currently running the script.
  • $_SERVER['HTTP_USER_AGENT']: The user agent string of the currently used browser.

The following is an example of using the $_SERVER variable:

<?php
    echo "The current script is running on ".$_SERVER[&#39;SERVER_NAME&#39;]."<br>";
    echo "The user's browser is ".$_SERVER['HTTP_USER_AGENT']."<br>";
    echo "The current script is ".$_SERVER['PHP_SELF'];
?>

In the above script, we show the use of the $_SERVER variable to obtain the server name and user agent of the current script running String and PHP file path.

4. $_REQUEST variable

The $_REQUEST variable is an array containing $_GET, $_POST and $_COOKIE variables. When using HTTP requests, the $_REQUEST variable has access to these values. Here is an example:

<form action="index.php" method="post">
    <input type="text" name="username">
    <input type="password" name="password">
    <input type="submit" value="Submit">
</form>

<?php
    $username = $_REQUEST['username'];
    $password = $_REQUEST['password'];
    echo "Username: $username <br> Password: $password";
?>

In the above form, we submit the username and password using the POST method. Then, we use the $_REQUEST variable to get the value of the input field.

Summary

In PHP, setting super global variables allows us to conveniently handle HTTP requests and transfer data. In this article, we take a deep dive into several commonly used super global variables, including $_GET, $_POST, $_SERVER, and $_REQUEST. Familiarity with these variables is an essential skill for developing web applications.

The above is the detailed content of Let’s talk in depth about superglobal variables in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn