PHP | fgets( ) Function Last Updated : 11 Jul, 2025 Comments Improve Suggest changes 3 Likes Like Report The fgets() function in PHP is an inbuilt function which is used to return a line from an open file. It is used to return a line from a file pointer and it stops returning at a specified length, on end of file(EOF) or on a new line, whichever comes first. The file to be read and the number of bytes to be read are sent as parameters to the fgets() function and it returns a string of length -1 bytes from the file pointed by the user. It returns False on failure. Syntax: fgets(file, length) Parameters Used: The fgets() function in PHP accepts two parameters. file : It specifies the file from which characters have to be extracted. length : It specifies the number of bytes to be read by the fgets() function. The default value is 1024 bytes. Return Value : It returns a string of length -1 bytes from the file pointed by the user or False on failure. Errors And Exceptions The function is not optimised for large files since it reads a single line at a time and it may take a lot of time to completely read a long file. The buffer must be cleared if the fgets() function is used multiple times. The fgets() function returns Boolean False but many times it happens that it returns a non-Boolean value which evaluates to False. Suppose there is a file named "gfg.txt" which consists of : This is the first line. This is the second line. This is the third line. Program 1 php <?php // file is opened using fopen() function $my_file = fopen("gfg.txt", "rw"); // Prints a single line from the opened file pointer echo fgets($my_file); // file is closed using fclose() function fclose($my_file); ?> Output: This is the first line. Program 2 php <?php //file is opened using fopen() function $my_file = fopen("gfg.txt", "rw"); // prints a single line at a time until end of file is reached while (! feof ($my_file)) { echo fgets($my_file); } // file is closed using fclose() function fclose($my_file); ?> Output: This is the first line. This is the second line. This is the third line. Reference: http://www.php.net/manual/en/function.fgets.php Create Quiz Comment S Shubrodeep Banerjee Follow 3 Improve S Shubrodeep Banerjee Follow 3 Improve Article Tags : Misc Web Technologies PHP PHP-function Explore PHP Tutorial 8 min read BasicsPHP Syntax 4 min read PHP Variables 5 min read PHP | Functions 8 min read PHP Loops 4 min read ArrayPHP Arrays 5 min read PHP Associative Arrays 4 min read Multidimensional arrays in PHP 5 min read Sorting Arrays in PHP 4 min read OOPs & InterfacesPHP Classes 2 min read PHP | Constructors and Destructors 5 min read PHP Access Modifiers 4 min read Multiple Inheritance in PHP 4 min read MySQL DatabasePHP | MySQL Database Introduction 4 min read PHP Database connection 2 min read PHP | MySQL ( Creating Database ) 3 min read PHP | MySQL ( Creating Table ) 3 min read PHP AdvancePHP Superglobals 6 min read PHP | Regular Expressions 12 min read PHP Form Handling 4 min read PHP File Handling 4 min read PHP | Uploading File 3 min read PHP Cookies 9 min read PHP | Sessions 7 min read Like