|
int feof(string resource)
實(shí)例如下:
復(fù)制代碼 代碼如下:
<?php
$fh = fopen("/home/www/data/users.txt", "rt");
while (!feof($fh)) echo fgets($fh);
fclose($fh);
?>
bool feof ( resource $handle ):Tests for end-of-file on a file pointer
這個(gè)php manual上面的原話。
為了方便,我以前都是這樣使用的
復(fù)制代碼 代碼如下:
<?php
// if file can not be read or doesn't exist fopen function returns FALSE
$file = @fopen("no_such_file", "r");
// FALSE from fopen will issue warning and result in infinite loop here
while (!feof($file)) {
}
fclose($file);
?>
確實(shí),這樣使用比較簡單。但是,如果上面的變量$file不是一個(gè)合法的file pointer 或者已經(jīng)被fclose關(guān)閉了的話。
那么在程序的第六行出,就會(huì)產(chǎn)生一個(gè)waring,并發(fā)生死循環(huán)。
為什么?
原因就是
Returns TRUE if the file pointer is at EOF or an error occurs (including socket timeout); otherwise returns FALSE.
所以,為了安全起見,最好在使用上面代碼的時(shí)候 加個(gè)判斷,is_resource 還是比較安全的。
php技術(shù):php feof用來識(shí)別文件末尾字符的方法,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。