• PHP Video Tutorials

PHP - Function fgets()



The fgets() function can return a line from an open file. This function stops returning on a new line at specified length or EOF, whichever comes first and returns false on failure.

Syntax

string fgets ( resource $handle [, int $length ] )

This function can return a string of up to length-1 bytes read from the file pointed to by handle.

Example-1

<?php
   $file = fopen("/PhpProject/sample.txt", "r");
   echo fgets($file);
   fclose($file);
?>

Output

tutorialspoint

Example-2

<?php
   $file = fopen("/PhpProject/sample.txt", "r");
 
   while(! feof($file)) {
      echo fgets($file). "\n";
   }
 
   fclose($file);
?>

Output

tutorialspoint

tutorix

php_function_reference.htm
Advertisements