Understanding the scope of variables in PHP

Source: Internet
Author: User
Understanding the scope of a variable

Scopes are scopes that a variable can use or be visible in a script. PHP has 6 basic scope rules:


Built-in super global variables can be used and visible anywhere in the script.

constants, once declared, can be globally visible, meaning that they can be used inside and outside the function.

Global variables declared in a script are visible throughout the script, but not inside the function.

When a variable used inside a function is declared as a global variable, its name is the same as the global variable name.

A variable created inside a function that is declared static cannot be visible outside the function, but can be persisted during multiple executions of the function

A variable created inside a function is local to the function, and when the function terminates, the variable does not exist.

$_get and $_post arrays, as well as some other special variables, have their own scope rules. These are called super global variables, and they can be used and visible anywhere, including internal and external functions. The complete list of super global variables is as follows:

$GLOBALS, an array of all global variables (like the global keyword, which will allow access to globals within a function) $_server, the server environment variable array $_get, the variable array $_post passed to the script by the GET method, Array of variables passed to the script via the Post method $_cookie,cookie variable array $_files, array of variables related to file uploads $_env, array of environment variables $_request, array of variables for all user input, including $_get, $_post, and $_ The input that the cookie contains (but does not include the PHP4.3.0 version after the $_files) $_session, the session variable array

The above is to understand the scope of variables in PHP content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.