PHP static静态局部变量和静态全局变量总结
1.不会随着函数的调用和退出而发生变化,不过,尽管该变量还继续存在,但不能使用它。倘若再次调用定义它的函数时,它又可继续使用,而且保存了前次被调用后留下的值 2.静态局部变量只会初始化一次 3.静态属性只能被初始化为一个字符值或一个常量,不能使用
1.不会随着函数的调用和退出而发生变化,不过,尽管该变量还继续存在,但不能使用它。倘若再次调用定义它的函数时,它又可继续使用,而且保存了前次被调用后留下的值
2.静态局部变量只会初始化一次
3.静态属性只能被初始化为一个字符值或一个常量,不能使用表达式。即使局部静态变量定义时没有赋初值,系统会自动赋初值0(对数值型变量)或空字符(对字符变量);静态变量的初始值为0。
4.当多次调用一个函数且要求在调用之间保留某些变量的值时,可考虑采用静态局部变量。虽然用全局变量也可以达到上述目的,但全局变量有时会造成意外的副作用,因此仍以采用局部静态变量为宜。
代码如下 复制代码
function test()
{
static $var = 5; //static $var = 1+1;就会报错
$var++;
echo $var . ' ';
}
test(); //2
test(); //3
test(); //4
echo $var; //报错:Notice: Undefined variable: var
关于静态全局变量
代码如下 复制代码
//全局变量本身就是静态存储方式,所有的全局变量都是静态变量
function static_global(){
global $glo;
$glo++;
echo $glo.'
';
}
static_global(); //1
static_global(); //2
static_global(); //3
echo $glo . '
'; //3
$a 将会在包含文件 b.inc 中生效。
代码如下 复制代码
$a = 1;
include "b.inc";
?>
//局部变量测试
$s1= "out s1"; //全局变量
function say(){
$s1 = "in s1"; //局部变量
echo "say():$s1";
}
say(); //输出局部变量: in s1
echo "
";
echo "function out:$s1"; ////输出全局变量:out s1
//static变量测试
function count1(){
$num = 0;
$num++;
echo $num." ";
}
function count2(){ //www.111cn.net
static $num = 0;
$num++;
echo $num." ";
}
for($i=0; $i count1(); //11 1 1 1 1 1 1 1 1 1
}
echo "
";
for($i=0; $i count2(); //1 2 3 4 5 6 7 8 9 10
}
echo "
";
//全局变量在函数中运用,加global
$a="php";
$b = "java";
function show(){
echo $a; // 无输出
global $b;
echo $b; //定义global,输出java
}
show();
?>
输出3
代码如下 复制代码
$a = 1;
$b = 2;
function Sum()
{
global $a, $b;
$b = $a + $b;
}
Sum();
echo $b; //3
?>
在全局范围内访问变量的第二个办法,是用特殊的 PHP 自定义 $GLOBALS 数组
代码如下 复制代码
$a = 1;
$b = 2;
function Sum()
{
$GLOBALS["b"] = $GLOBALS["a"] + $GLOBALS["b"];
}
Sum();
echo $b;
?>
静态变量也提供了一种处理递归函数的方法。递归函数是一种调用自己的函数
代码如下 复制代码
function Test()
{
static $count = 0;
$count++;
echo $count; //12345678910
if ($count Test ();
}
//$count--;
}
Test();
总结
局部变量——用在函数内,作用域就是所在函数
全局变量——在函数外,作用域在整个php文件(包含了 include 和 require 引入的文件),但在函数中不能读到,除非重新申明为global
静态变量——用在函数内,被调用完后,内存不释放,保留最后值,多用来统计累加。
更多详细内容请查看:http://www.111cn.net/phper/php/57862.htm

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
