Feof funksiyası

feof funksiyası oxu zamanı faylın sonuna catilib-catilmadigini yoxlayir. O, bir parametr qebul edir - fopen funksiyası terefinden ugurla acilmis fayl gostericisi. Faylın sonuna catilmissa true, eks halda ise false qaytarir.

Sintaksis

phpfeof(resource $handle): bool

Numune

Oxu zamanı faylın sonuna catilib-catilmadigini yoxlayaq:

php<?php
  $file = fopen('test.txt', 'r');
  
  while (!feof($file)) {
    echo fgets($file);
  }
  
  fclose($file);
?>

Bu numunede faylın sonuna catana qeder onu setir-setir oxuyuruq.

Numune

Fayl acildiqdan sonra fayl gostericisinin veziyyetini yoxlayaq:

php<?php
  $file = fopen('empty.txt', 'r');
  var_dump(feof($file));
  fclose($file);
?>

Bos fayl ucun kodun icra neticesi:

phptrue

Numune

Fayl acarken xetanın emalı:

php<?php
  $file = @fopen('nonexistent.txt', 'r');
  
  if ($file === false) {
    echo "File not found";
  } else {
    while (!feof($file)) {
      echo fgets($file);
    }
    fclose($file);
  }
?>

Fayl movcud olmadıqda kodun icra neticesi:

php'File not found'

Həmçinin bax

  • fread funksiyası,
    fayldan oxuyur
  • fgets funksiyası,
    fayldan setir oxuyur
  • rewind funksiyası,
    fayl gostericisini sıfırlayır