php8.5
Home/ Manual/ spl / splfileobject/ SplFileObject::fgets

SplFileObject::fgets

PHP function Edit on GitHub ✎

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

Gets line from file

Description

SplFileObject::fgets(): string

Gets a line from the file.

Parameters Parameters

Return Values

Returns a string containing the next line from the file.

Errors/Exceptions

Throws a RuntimeException if the file cannot be read.

Examples

SplFileObject::fgets example

This example simply outputs the contents of file.txt line-by-line.

php
<?php
$file = new SplFileObject("file.txt");
while (!$file->eof()) {
    echo $file->fgets();
}
?>

See Also

  • fgets()
  • SplFileObject::fgetss
  • SplFileObject::fgetc
  • SplFileObject::current

Source: reference/spl/splfileobject/fgets.xml · from the official PHP manual (php/doc-en)