Home > Backend Development > PHP Tutorial > Detailed explanation of how to use php variables_PHP tutorial

Detailed explanation of how to use php variables_PHP tutorial

WBOY
Release: 2016-07-13 10:44:49
Original
1075 people have browsed it

Variables in PHP are divided into five types: local variables, variable functions, global variables, dynamic variables, and dynamic variables. Now I will give you a detailed introduction to the use of these five PHP variables. Friends who need to know more Available for reference.

Static variables

The storage space of dynamic variables will be automatically released after the function is executed. For static variables, when the function is executed, the storage space still stores its variables. If you want to use the variable results of the function to perform calculations, you need to set the variable as a static variable.

The way to set PHP static variables is to add the "static" symbol before the variable.

Example:

The code is as follows Copy code
 代码如下 复制代码

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

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

Dynamic variables

When we define a variable, it is equivalent to applying for a storage space on the server. Dynamic variables are quantities whose values ​​are constantly changing. The characteristic is that when the function is completed, the value of its variable storage space will be automatically released. Just like a truck pulling goods, unloading it after it reaches the destination, and then returning to pull it.

Example:
 代码如下 复制代码

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

The code is as follows Copy code

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

Global variables

We learned earlier about local variables, whose values ​​are only valid within a certain range. If you want to implement cross-domain calls, you need to use global variables.

Definition of global variables in PHP: To declare a global variable, just add "global" before the variable name. After defining the global variable, the variables of the main program can be called inside the function. Similarly, the variables within the function can also be called. Used by the main program.

 代码如下 复制代码

$a=1;
function fun(){
echo $a ."
"; /* $a为局部变量,不显示内容 */
  global $a;  /* 定义$a为全局变量 */
  echo $a ."
";
  global $b;
  $b=10;
 }
 fun();
 echo $b;
?>

Example 1:

The code is as follows Copy code
$a=1;
function fun(){
echo $a ."
"; /* $a is a local variable and does not display the content */
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 they leave, the variables will lose their meaning or change, which is called variable scope. Just like a local area network, it is only valid in the established office or dormitory. Once it exceeds this range, it will not be able to connect to the network. Variables can be divided into local variables and global variables according to their scope. Here we will talk about local variables first.

PHP local variable value Variables defined within the function are only used within the function and are invalid outside the function; similarly, variables defined outside the function are not valid within the function if they are not passed.
 代码如下 复制代码
function fun(){
$a1=5;
echo $a2;
}
fun();
$a2=10;
echo $a1;
?>
Example:

The above code outputs nothing after running.

Variable function

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

Example:

The code is as follows
 代码如下 复制代码

function p($name){
echo "my name is ".$name;
}
$someone="p"; /* 将字符串"p"赋给$someone */
$someone('Tom'); /* 这里等价于$p('Tom'),是一个变量函数 */
?>

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

http://www.bkjia.com/PHPjc/633072.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/633072.html
TechArticle
Variables in php are divided into five types: local variables, variable functions, global variables, dynamic variables, and dynamic variables. Now let me give you a detailed introduction to the use of these five php variables...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template