Home > Backend Development > PHP Tutorial > How to use super global variables in PHP programming?

How to use super global variables in PHP programming?

PHPz
Release: 2023-06-12 11:38:02
Original
767 people have browsed it

Super global variables are a very powerful and common variable type in PHP programming. They store various information related to the running status of the program, such as user input, server environment variables, etc. In this article, we will introduce how to use PHP super global variables, as well as some common super global variables and their functions.

1. The concept and function of super global variables
Super global variables are predefined variables in PHP. They can be used anywhere in the script without scope restrictions. Super global variables are used to store information in the program running environment, such as HTTP request information, server information, etc. when the script is executed. The predefined super global variables in PHP are as follows:

  1. $GLOBALS: contains an array of all global variables;
  2. $_SERVER: contains server environment variable information;
  3. $_GET: Contains the contents of variables passed through the HTTP GET method;
  4. $_POST: Contains the contents of the variables passed through the HTTP POST method;
  5. $_COOKIE: Contains the contents of variables passed through HTTP Cookies;
  6. $_SESSION: Contains the contents of variables in the current session;
  7. $_FILES: Contains information about uploaded files.

2. Use super global variables
In PHP programming, using super global variables is very simple. You only need to use the super global variable name directly in the code. The following is sample code for using super global variables:

  1. Use $_GET to get the value of a variable passed through the HTTP GET method:
<?php
    $name = $_GET['name'];
    echo "Hello, $name!";
?>
Copy after login
  1. Use $_POST to get Values ​​of variables passed via HTTP POST method:
<?php
    $name = $_POST['name'];
    echo "Hello, $name!";
?>
Copy after login
  1. Use $_SESSION to save and get values ​​of session variables:
<?php
    session_start(); // 开始会话
    $_SESSION['username'] = 'john'; // 保存会话变量
    echo $_SESSION['username']; // 获取会话变量
?>
Copy after login
  1. Use $_FILES Get relevant information about uploaded files:
<?php
    $filename = $_FILES['file']['name'];
    $filetype = $_FILES['file']['type'];
    $filesize = $_FILES['file']['size'];
    echo "Filename: $filename<br>";
    echo "File type: $filetype<br>";
    echo "File size: $filesize bytes";
?>
Copy after login

3. Notes

  1. Super global variables are predefined variables, they cannot be redefined or initialized.
  2. When using super global variables, the security of the input value must be ensured. Because the values ​​of these variables are passed by the user or client, security is very important. You can use the filter functions provided by PHP or third-party libraries to ensure the security of input values.
  3. Super global variables may have different values ​​or behaviors in different environments. Therefore, when using super global variables, pay attention to the environment in which they are located.

In short, super global variables are a very powerful and common variable type in PHP programming. They can easily obtain and process the running environment information of the program, such as user input, server environment variables, etc. When using super global variables, you must pay attention to the safety of the input values ​​to ensure the safety and stability of the program.

The above is the detailed content of How to use super global variables in PHP programming?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template