Php variable scope usage summary

Source: Internet
Author: User
Php variable scope usage summary

  1. Function function_name (){
  2. Global $ var;
  3. }
  4. $ Var = 20;
  5. Function_name (); // Function call.
  6. ?>

In this example, $ var inside the function is now the same as $ var outside the function. This means that the variable $ var already has a value of 20. if this value is changed inside the function, the external $ var value will also change. Another way to avoid variable scopes is to use super-global variables such as $ _ GET, $ _ POST, and $ _ REQUEST. These variables are automatically accessible in your function (therefore, they are super global variables ). You can also add elements to the $ GLOBALS array so that they can be used in the function.

We recommend that you do not use global variables in functions. When designing functions, make them accept each value as a parameter as needed and return any value as needed. Relying on the global variables in the function makes them more context-dependent and thus less useful.

In PHP, variables mainly include: Built-in super global variables, General variables, constants, global variables, static variables, and so on.

The built-in Super global variables can be used and visible anywhere in the script. That is, if we change a value in a PHP page, the value will also change when used on other PHP pages.

Once declared, constants can be globally visible, that is, they can be used inside and outside the function, but this is limited to the PHP scripts included in a page (including include and include_once, but it cannot be used in other pages.

The global variables declared in a script are visible throughout the script, but not inside the function. if the variables in the function are the same as the global variable name, take the internal variables of the function as the standard.

When the variable used in the function is declared as a global variable, its name must be consistent with that of the global variable. in this case, we can use global variables outside the function, in this way, we can avoid the situation where the external variables are overwritten because the internal variables of the function are the same as the external global variables.

Variables created and declared as static in a function cannot be visible outside the function, but they can be kept during multiple function executions, the most common case is the recursive execution of functions.

The variable created in the function is local to the function. when the function is terminated, the variable does not exist.

The complete list of Super global variables is as follows:

  1. $ A = 4;
  2. Function sendValue ($ x)
  3. {
  4. Echo $ x;
  5. }
  6. SendValue ($ );
  7. ?>

Description: $ a is defined outside the function. a function defines parameters. when a function is called, $ a is passed as a parameter. Therefore, the above code can run normally.

  1. $ A = 4;
  2. Function sendValue ()
  3. {
  4. Echo $;
  5. }
  6. SendValue ();
  7. ?>

Description: When a function is called, $ a cannot be passed as a parameter. Therefore, the above code cannot run normally. The scope of a variable is the context defined by the variable ). Most PHP variables have only one separate range. This separate range span also contains the files introduced by include and require. Example:

  1. $ A = 1;
  2. Include "B. inc ";
  3. ?>

The variable $ a will take effect in the included file B. inc. However, in user-defined functions, a local function range will be introduced. By default, any variable used in a function is limited to a local function. Example:

  1. $ A = 1;/* global scope */

  2. Function Test ()

  3. {
  4. Echo $ a;/* reference to local scope variable */
  5. }

  6. Test ();

  7. ?>

This script does not have any output, because the echo statement references a local version of variable $ a and is not assigned a value within this range. You may notice that the global variables in PHP are a little different from those in C language. in C language, global variables automatically take effect in functions unless they are overwritten by local variables. This may cause some problems. some may carelessly change a global variable. Global variables in PHP must be declared as global when used in functions.

The global keyword is an example of using global:

Example 12-1. use global

  1. $ A = 1;

  2. $ B = 2;

  3. Function Sum ()

  4. {
  5. Global $ a, $ B;

  6. $ B = $ a + $ B;

  7. }

  8. Sum ();

  9. Echo $ B;
  10. ?>

The output of the above script is "3 ". Specify the global variables $ a and $ B in the function. all referenced variables of any variable point to the global variable. PHP has no limit on the maximum number of global variables that a function can declare.

The second way to access variables globally is to use a special PHP custom $ GLOBALS array. The preceding example can be written as follows:

Example 12-2. replace global with $ GLOBALS

  1. $ A = 1;

  2. $ B = 2;

  3. Function Sum ()

  4. {
  5. $ GLOBALS ["B"] = $ GLOBALS ["a"] + $ GLOBALS ["B"];
  6. }

  7. Sum ();

  8. Echo $ B;
  9. ?>

In the $ GLOBALS array, each variable is an element. The key name corresponds to the variable name and value variable content. $ GLOBALS exists globally because $ GLOBALS is a super global variable. The following example shows how to use a super global variable:

Example 12-3. Examples of hyperglobal variables and scopes

  1. Function test_global ()

  2. {
  3. // Most of the predefined variables are not "super". they need to use the 'global' keyword to make them valid in the local area of the function.
  4. Global $ HTTP_POST_VARS;

  5. Print $ HTTP_POST_VARS ['name'];

  6. // Superglobals are valid in any range and they do not require 'global' declarations. Superglobals was introduced in PHP 4.1.0.

  7. Print $ _ POST ['name'];
  8. }
  9. ?>

Another important feature of using the range of static variables is static variable ). Static variables only exist in local function domains, but their values are not lost when the program runs out of this scope. Take a look at the following example:

Example 12-4. example of static variables

  1. Function Test ()
  2. {
  3. $ A = 0;
  4. Echo $;
  5. $ A ++;
  6. }
  7. ?>

This function is useless, because every call will set the value of $ a to 0 and output "0 ". Adding $ a ++ to the variable does not work, because $ a does not exist once you exit this function. To write a counting function that does not lose the current count value, you must define variable $ a as static:

Example 12-5. example of using static variables

  1. Function Test ()
  2. {
  3. Static $ a = 0;
  4. Echo $;
  5. $ A ++;
  6. }
  7. ?>

Now, every time you call the Test () function, the value of $ a is output and added with one.12. Next page

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.