扫码关注官方订阅号
"; } // 第一次执行,$nm = 2 test(); // 第一次执行,$nm = 4 test(); // 第一次执行,$nm = 8 test(); ?>
如何能调用方法 循环生成 2 4 6 2 4 6;
认证0级讲师
<?php function test() { $config = [2, 4, 6]; static $i = 0; $result = $config[$i]; if ($i >= count($config) - 1) { $i = 0; } else { $i++; } echo $result . '<br />'; } test(); test(); test(); test(); test(); test();
更优雅的实现:
function test() { static $value = [2, 4, 6]; $tmp = array_shift($value); echo $tmp . "\n"; array_push($value, $tmp); test(); } test();
((i % 3)+1)*2 i++
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
更优雅的实现: