Timer创建定时器设置变量没秒自减,当变量符合条件时处理其他逻辑,怎么实现?(二楼事例)

MyLover

测试得知:php文件记事本打开另存为ansi格式,发送或接收的中文字符串才能正常显示,否则就是乱码的。
可不可以改成utf8格式也能正常显示中文呢?

3285 2 0
2个回答

MyLover

timer添加定时器:

$time_init = 300;
$timerId = Timer::add(1, function($time_init){
    echo "time:".$time_init.",status=0\n";
    $time_init -- ;
    if ($time_init <= 295 && $time_init > 290){
        echo "time:".$time_init.",status=1\n";
    }else if ($time_init <= 290){
        echo "time:".$time_init.",status=2\n";
    }
},array($time_init));

怎样才能做到 $time_init 每秒自减呢?现在这样打印出来 $time_init 一直是300,没变~

  • 暂无评论
walkor 打赏
$time_init = 300;
$timerId = Timer::add(1, function() use (&$time_init){
    echo "time:".$time_init.",status=0\n";
    $time_init -- ;
    if ($time_init <= 295 && $time_init > 290){
        echo "time:".$time_init.",status=1\n";
    }else if ($time_init <= 290){
        echo "time:".$time_init.",status=2\n";
    }
}));

或者

$time_init = 300;
$timerId = Timer::add(1, function(){
    global $time_init;
    echo "time:".$time_init.",status=0\n";
    $time_init -- ;
    if ($time_init <= 295 && $time_init > 290){
        echo "time:".$time_init.",status=1\n";
    }else if ($time_init <= 290){
        echo "time:".$time_init.",status=2\n";
    }
});

或者

$timerId = Timer::add(1, function(){
    static $time_init = 300;
    echo "time:".$time_init.",status=0\n";
    $time_init -- ;
    if ($time_init <= 295 && $time_init > 290){
        echo "time:".$time_init.",status=1\n";
    }else if ($time_init <= 290){
        echo "time:".$time_init.",status=2\n";
    }
});

。。。

  • MyLover 2015-06-19

    3Q,那编码问题怎么处理呢?难道只能用gbk么?

  • walkor 2015-06-19

    编码都建议用utf8,显示乱码是显示终端没设置好编码

年代过于久远,无法发表回答
×
🔝