feof

  • The feof function tests if a file pointer has reached end of file (EOF).

    This example demonstrates by making a dummy read, which places the pointer at the end of the file. The result of feof is then true.

    PHP

    $f = fopen('/path/to/foobar', 'r');
    fread($f);
     
    var_export( feof($f) );
    //=> true
     
    fclose($f);

    In Ruby, the File#eof instance method works the same way.

    Ruby

    File.open('/path/to/foobar', 'r') do |f|
      f.read
     
      p f.eof
      #=> true
    end

    The File#eof instance method is also aliased as File#eof?. It’s a common Ruby idiom for methods that return boolean be written like this, as if asking a question.

    Both File#eof and File#eof? are commonly used, so choose the one that is most expressive for your usage.

fclose file

Looking for Rails or PHP web application development, integration, and training?

Rails for PHP is brought to you by Maintainable Software. Get custom web applications and personalized training from the authors of the book and website.