<%! static class Count { private static int count = 0; public static int getCount(){ count++; return count; } } %> <% out.print(Count.getCount()); %> 通过浏览器连续访问,会分别输出:1,2,3,4,5,6,........
<?php class Count{ private static $count = 0; public static function getCount(){ self::$count++; return self::$count; } } echo Count::getCount(); ?> 通过浏览器连续访问,会分别输出:1,1,1,1,1,1,........
总结
1、java的static变量伴随着java虚拟机的退出而消亡,java虚拟机运行期间,static变量一直存在。
2、php的static变量只针对一次请求(一次php文件的执行),php文件执行完毕,该static变量也随机消亡,再次请求(再次执行该php文件),会重新创建该static变量。
立即学习“PHP免费学习笔记(深入)”;
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号