How to use PHP variables _php tutorial

Source: Internet
Author: User
Tags variable scope
In PHP variables are divided into local variables, variable functions, global variables, dynamic variables, dynamic variables Five, let me give you a detailed introduction of these five kinds of PHP variables to use the method, there is a need to understand the friends can refer to.

static variables

Dynamic variables when the function finishes executing, its storage space is automatically freed. Static variables, when the function is executed, still store their variables. If you want to take advantage of the function's variable result in the calculation, you need to set the variable to a static variable.

PHP static variables are set by adding a "static" symbol before the variable.

Instance:

The code is as follows Copy Code

function Fun () {
static $i = 0;
echo "$i
";
$i + +;
}
Fun ();
Fun ();
?>

Dynamic variables

When we define a variable, it is equivalent to requesting a storage space on the server. Dynamic variable is the amount of constant change in value, the characteristic is that when the function is completed, the value of its variable storage space will be automatically released. For example, the truck pull, shipped to the destination after unloading, and then back in the pull.

Instance:

The code is as follows Copy Code

function Fun () {
$i = 0;
echo "$i
";
$i + +;
}
Fun ();
Fun ();
?>

Global variables

A local variable is learned earlier, its value is only valid within a certain range, and a global variable is used if you want to implement a cross-domain call.

PHP defines global variables: the declaration of global variables as long as the variable name with "global" can be, after defining the global variables, the main program's variables can be called inside the function, the same function of the variables can be used by the main program.

Example 1:

The code is as follows Copy Code

$a = 1;
function Fun () {
echo $a. "
"; /* $a as local variable, no content displayed */
Global $a; /* Define $ A as a global variable */
echo $a. "
";
Global $b;
$b = 10;
}
Fun ();
Echo $b;
?>

Local variables

In the PHP language, variables have a certain scope, once left, the variable will lose meaning or change, called variable scope. As if the local area network, only in the establishment of the office or the dormitory is valid, once beyond this range will not connect to the network. The scope of a variable can be divided into local variables and global variables, where local variables are first spoken.

Variables defined within the PHP local variable value function are used only within the function and are not valid outside the function; Similarly, variables defined outside the function are not passed and are not valid within the function.

Instance:

The code is as follows Copy Code
function Fun () {
$a 1=5;
echo $a 2;
}
Fun ();
$a 2=10;
echo $a 1;
?>

Nothing is output after the above code is run.

Variable function

Variables can be defined not only by some characters such as the English alphabet, but also by the function. If you add a variable name to the "()" Symbol, PHP will look for a function that is the same as the value of the variable, called the variable function.

Instance:

The code is as follows Copy Code

function P ($name) {
echo "My name is". $name;
}
$someone = "P"; /* Assign the string "P" to $someone */
$someone (' Tom '); /* Here is equivalent to $p (' Tom '), which is a variable function */
?>

http://www.bkjia.com/PHPjc/633072.html www.bkjia.com true http://www.bkjia.com/PHPjc/633072.html techarticle in PHP, variables are divided into local variables, variable functions, global variables, dynamic variables, dynamic variables Five, let me give you a detailed description of these five kinds of PHP variables to use the party ...

  • 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.