Fungsi feof
Fungsi feof menyemak sama ada hujung fail telah dicapai semasa membaca. Ia menerima satu parameter - penunjuk fail yang telah berjaya dibuka oleh fungsi fopen. Mengembalikan true jika hujung fail dicapai, dan false sebaliknya.
Sintaks
feof(resource $handle): bool
Contoh
Semak sama ada hujung fail telah dicapai semasa membaca:
<?php
$file = fopen('test.txt', 'r');
while (!feof($file)) {
echo fgets($file);
}
fclose($file);
?>
Dalam contoh ini, kita membaca fail baris demi baris sehingga mencapai hujungnya.
Contoh
Semak keadaan penunjuk fail selepas membuka fail:
<?php
$file = fopen('empty.txt', 'r');
var_dump(feof($file));
fclose($file);
?>
Keputusan pelaksanaan kod untuk fail kosong:
true
Contoh
Pemprosesan ralat semasa membuka fail:
<?php
$file = @fopen('nonexistent.txt', 'r');
if ($file === false) {
echo "File not found";
} else {
while (!feof($file)) {
echo fgets($file);
}
fclose($file);
}
?>
Keputusan pelaksanaan kod jika fail tidak wujud:
'File not found'