Easy Tutorial
❮ Func Curl_Share_Init Php Spaceship Operator ❯

PHP fgets() Function


Complete PHP Filesystem Reference Manual


Definition and Usage

The fgets() function returns a line from an open file.

The fgets() function stops returning a new line when it reaches the specified length (length - 1), encounters a newline character, or reads to the end of the file (EOF)—whichever comes first.

If it fails, the function returns FALSE.

Syntax

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

Example 1

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

The above code will output:

Hello, this is a test file.

Example 2: Reading a File Line by Line

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

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

fclose($file);
?>

The above code will output:

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

Complete PHP Filesystem Reference Manual

❮ Func Curl_Share_Init Php Spaceship Operator ❯