Php variable usage

Source: Internet
Author: User
In php, variables are divided into five types: local variables, variable functions, global variables, dynamic variables, and dynamic variables. next I will give you a detailed introduction to the static usage of these five php variables.

In php, variables are divided into five types: local variables, variable functions, global variables, dynamic variables, and dynamic variables. next I will introduce these five types of php variables in detail.

Static variables

Dynamic variables are automatically released after the function is executed, while static variables are still stored after the function is executed, if you want to use the function's variable results for calculation, you need to set the variable to a static variable.

The php static variable setting method is to add the "static" symbol before the variable.

The instance code is as follows:

  1. Function fun (){
  2. Static $ I = 0;
  3. Echo "$ I
    ";
  4. $ I ++;
  5. }
  6. Fun ();
  7. Fun ();
  8. ?>

Dynamic variables,When we define a variable, it is equivalent to applying for a bucket on the server. Dynamic variables are the changing amount of values. they are characteristic that the value of the variable storage space is automatically released after the function is run. For example, a freight car pulls the goods, unload the goods after it is shipped to the destination, and then return to the pulling.

The instance code is as follows:

  1. Function fun (){
  2. $ I = 0;
  3. Echo "$ I
    ";
  4. $ I ++;
  5. }
  6. Fun ();
  7. Fun ();
  8. ?>

Global variables:We have learned about local variables. Their values are valid only within a certain range. if you want to implement cross-origin calls, you need to use global variables.

Php defines global variables:To declare a global variable, you only need to add "global" before the variable name. after the global variable is defined, the main program variable can be called internally by the function, the variables in the same function can also be used by the main program.

The code for instance 1 is as follows:

  1. $ A = 1;
  2. Function fun (){
  3. Echo $ ."
    ";/* $ A is a local variable and the content is not displayed */
  4. Global $ a;/* defines $ a as a global variable */
  5. Echo $ ."
    ";
  6. Global $ B;
  7. $ B = 10;
  8. }
  9. Fun ();
  10. Echo $ B;
  11. ?>

Local variables:In php, variables have a certain scope of function. once left, variables will lose meaning or change, which is called the scope of variables. It is like a LAN, which is only valid in the established office or dormitory. Once the LAN is out of this range, the network will not be connected. The variables can be divided into local variables and global variables. here we will talk about local variables first.

The variables defined in the php local variable value function are only used in the function and are invalid outside the function. Similarly, if the variables defined outside the function are not passed, they are also invalid in the function.

The instance code is as follows:

  1. Function fun (){
  2. $ A1 = 5;
  3. Echo $ a2;
  4. }
  5. Fun ();
  6. $ A2 = 10;
  7. Echo $ a1;
  8. ?>

Nothing is output after the above code is run.

Variable functions:Variables can be defined not only by English letters or other characters, but also by using the "()" symbol after a variable name, php will look for function execution with the same value as the variable, which is called a variable function.

The instance code is as follows:

  1. Function p ($ name ){
  2. Echo "my name is". $ name;
  3. }
  4. $ Someone = "p";/* assign the string "p" to $ someone */
  5. $ Someone ('Tom ');/* it is equivalent to $ p ('Tom') and is a variable function */
  6. ?>

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.