输出 "0"。将变量加一的 $w3sky++ 没有其到效果,因为一旦退出本函数则变量 $w3sky 就不存在了。要写一个不会丢失本次计数值的计数函数,要将变量 $w3sky 定义为静态(static)的:
<?php
function Test()
{
$w3sky = 0;
echo $w3sky;
$w3sky++;
}
?>
本函数每次调用时都会将 $w3sky 的值设为 0 并
输出 $w3sky 的值并加一。
<?php
function Test()
{
static $w3sky = 0;
echo $w3sky;
$w3sky++;
}
?>
本函数每调用Test()都会
<?PHP
function Test()
{
static $count = 0;
$count++;
echo $count;
if ($count < 10) {
Test();
}
$count--;
}
?>
注: 静态变量可以按照上面的例子声明。如果在声明中用表达式的结果对其赋值会导致解析错误。
声明静态变量例子:
代码如下:
<?PHP
function foo(){
static $int = 0;// correct
static $int = 1+2; // wrong (as it is an expression)
static $int = sqrt(121); // wrong (as it is an expression too)
$int++;
echo $int;
}
?>
Copyright © 2019- iaaf.cn 版权所有
违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务