怎样每次刷新页面时都让$a加一呢?
phpcn_u238
phpcn_u238 2017-02-03 16:55:03
0
2
1040

我在当前页面的控制器里面写的  $name也都有值 我就是想刷新一次当前页面后就执行一次这个count函数 这样写为什么不行呢?这样写每次得到的都是同一个值

$name=I('t');
function count(){
    static $a=0;
    ++$a;
    echo $a;
}
if(!empty($name)){
   count();
}

                                           


phpcn_u238
phpcn_u238

reply all(2)
数据分析师

How to increase $a by one every time the page is refreshed? -PHP Chinese website Q&A-How to increase $a by one every time the page is refreshed? -PHP Chinese website Q&A

Let’s take a look and learn.

阿神
$name=I('t');
function count(){
  // 首先判断session中是否已设置名为a的值
  if(session('?a')) {  // 此行用原生php也可写成 if(isset( $_SESSION['a'] ))
    $a = session('a'); // 已经设置,则从session中取出值
  }else{
    $a=session('a',0); // 没有设置,则设置初始值0
  }
    $a++;
    echo $a;

    session('a', $a); // 把a的值保存到session中以便下次使用。
}
if(!empty($name)){
  count();
}

仅供参考

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!