Current Location: Home> Function Categories> fgets

fgets

Read a line from a file pointer
Name:fgets
Category:File system
Programming Language:php
One-line Description:Return a line from the opened file.

Definition and usage

The fgets() function reads a line from a file pointer.

实例

例子 1

<?php

$file = fopen("test.txt","r");
echo fgets($file);
fclose($file);

?>

输出类似:

Hello, this is a test file.

例子 2

<?php

$file = fopen("test.txt","r");

while(! feof($file))
  {
  echo fgets($file). "<br />";
  }

fclose($file);
?>

输出类似:

Hello, this is a test file. 
There are three lines here. 
This is the last line.

grammar

 fgets ( file , length )
parameter describe
file Required. Specifies the file to be read.
length Optional. Specifies the number of bytes to be read. The default is 1024 bytes.

illustrate

Read a line from the file pointed to by the file and return a string of length up to length - 1 byte. It encounters a newline character (including in the return value), EOF, or stops after length - 1 byte has been read (it depends on which case it encounters first). If length is not specified, the default is 1K, or 1024 bytes.

If it fails, return false.

Similar Functions
Popular Articles