Php variable usage

Source: Internet
Author: User

Static variables

After a function is executed, its storage space is automatically released. Static variables are stored in the bucket 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.

Instance:

The code is as follows: Copy code

<? Php
Function fun (){
Static $ I = 0;
Echo "$ I <br> ";
$ I ++;
 }
Fun ();
Fun ();
?>

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.

Instance:

The code is as follows: Copy code

<? Php
Function fun (){
$ I = 0;
Echo "$ I <br> ";
$ I ++;
 }
Fun ();
Fun ();
?>

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: you only need to add "global" to the declaration of global variables. After the global variables are defined, the variables of the main program can be called internally by the function, the variables in the same function can also be used by the main program.

Instance 1:

The code is as follows: Copy code

<? Php
$ A = 1;
Function fun (){
Echo $ a. "<br>";/* $ a is a local variable and no content is displayed */
Global $ a;/* defines $ a as a global variable */
Echo $ a. "<br> ";
Global $ B;
$ B = 10;
 }
Fun ();
Echo $ B;
?>

Local variable

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.

Instance:

The code is as follows: Copy code
<? Php
Function fun (){
$ A1 = 5;
Echo $ a2;
 }
Fun ();
$ A2 = 10;
Echo $ a1;
?>

 

Nothing is output after the above code is run.

Variable functions

Variables can be defined not only by English letters and other characters, but also by functions. If you add the "()" symbol after a variable name, php searches for function execution with the same value as the variable, which is called a variable function.

Instance:

The code is as follows: Copy code

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

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.