PHP 中的feof方法

bool feof ( resource $handle ): Tests for end-of-file on a file pointer
 
这个php manual上面的原话。
为了方便,我以前都是这样使用的
 
   
1 <? php
2   // if file can not be read or doesn't exist fopen function returns FALSE
3   $file = @ fopen ( " no_such_file " , " r " );
4
5 // FALSE from fopen will issue warning and result in infinite loop here
6 while ( ! feof ( $file )) {
7 }
8
9 fclose ( $file );
10 ?>

确实,这样使用比较简单。但是,如果上面的变量$file不是一个合法的file pointer 或者已经被fclose关闭了的话。

那么在程序的第六行出,就会产生一个waring,并发生死循环。

为什么?

原因就是

Returns TRUE if the file pointer is at EOF or an error occurs (including socket timeout); otherwise returns FALSE.

所以,为了安全起见,最好在使用上面代码的时候 加个判断,is_resource 还是比较安全的。

 

 

转载于:https://www.cnblogs.com/ainiaa/archive/2010/08/01/1789709.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值