Home PHP Filesystem fgets() PHP fgets() Function Definition In PHP, fgets() reads one line from an open file. It returns the line as a string, including the line break at the end, or false when nothing is left to read.
Call it again and again to move through a text file line by line. It is the most common function for reading log files, text lists and any file you want to process one line at a time.
Syntax Call fgets() with the values you need. Start with this line.
Call What it does fgets($handle)Reads the next full line, including the newline character. fgets($handle, 6)Reads at most 5 bytes (length minus one) or up to the end of the line. while (($line = fgets($handle)) !== false) { ... }Reads the whole file line by line.
The returned string still contains the line break, so use trim() or rtrim() when you print or compare it. When you pass a length, fgets() reads at most length - 1 bytes.
Library declarations fgets(resource $stream, ?int $length = null): string|false Parameters Parameter Description stream Required. An open file stream, such as the one returned by fopen(). It must be open for reading. length Optional. Reading stops after length - 1 bytes. The default null reads until the end of the line, with no limit.
Return Value Return Value Description string The line that was read, including the line break if there is one. false Returned at the end of the file or on failure.
Important
Each line keeps its newline, so "Apple" is really "Apple" plus a line break. Use trim() before you compare or print values. Always test the result with !== false, because a line such as "0" is falsy in PHP.
PHP Examples 1. Read the First Line 2. Limit How Much Is Read 3. Number Every Line Example 1 — Read the First Line Write three lines to a file and read only the first one.
<?php
$path = sys_get_temp_dir() . '/codetofun_fgets.txt';
file_put_contents($path, 'Apple' . PHP_EOL . 'Banana' . PHP_EOL . 'Cherry' . PHP_EOL);
$handle = fopen($path, 'r');
$line = fgets($handle);
echo 'First line: ', trim($line), PHP_EOL;
fclose($handle);
unlink($path); How It Works file_put_contents() creates a file with three fruit names, one per line. fopen() opens the file for reading. fgets() reads only the first line, which is Apple plus its line break. trim() removes the line break before the text is printed. Example 2 — Limit How Much Is Read The second argument limits the bytes that are read. fgets() returns length minus one bytes at most.
<?php
$path = sys_get_temp_dir() . '/codetofun_fgets_edge.txt';
file_put_contents($path, 'Hello World');
$handle = fopen($path, 'r');
echo '[', fgets($handle, 6), ']', PHP_EOL;
echo '[', fgets($handle, 6), ']', PHP_EOL;
echo '[', fgets($handle, 6), ']', PHP_EOL;
fclose($handle);
unlink($path); How It Works The file has a single line, Hello World, with no line break. With a length of 6, fgets() reads at most 5 bytes, so the first call gives Hello. The second call continues where the first stopped and returns a space plus Worl. The third call returns the last letter d, because the end of the file is reached. Frequently Asked Questions What does fgets() do in PHP? It reads one line from an open file and returns it as a string. It returns false when there is nothing left to read.
Does fgets() include the newline character? Yes. The returned string ends with the line break, so use trim() or rtrim() to remove it before printing or comparing.
How do I read a whole file with fgets()? Use while (($line = fgets($handle)) !== false) { ... }. The loop ends when fgets() returns false at the end of the file.
What does the length parameter of fgets() do? It limits the read to length - 1 bytes. For example, fgets($handle, 6) reads at most 5 bytes, or fewer if the line is shorter.
What is the difference between fgets() and file()? fgets() reads one line at a time and uses little memory. file() loads every line into an array at once, which is simpler for small files.
Next: fgetss() fgetss() used to read one line from a file and strip the HTML and PHP tags from it. The fu.
fgetss() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion