Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

fgets()

Example

Read a single line from the open file.

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

Definition and Usage

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

Syntax

fgets(filelength)

Parameter Values

 

Parameter

Description

file

Mandatory. Specifies the open file from which to return a line.

length

Optional. Specifies the number of bytes to read. Reading stops when length - 1 bytes have been read, a new line is encountered, or end-of-file (EOF) is reached. If no length is specified, it reads until the end of the line.

Technical Details

Return Value:

Returns a single line read from the file on success, or FALSE if end-of-file (EOF) is reached or an error occurs.

PHP Version:

4.0+

Binary Safe:

Yes, in PHP 4.3

More Examples

Example

Read the open file one line at a time.

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

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

fclose($file);
?>