Change language

PHP Fgets () Function

PHP fgets() function - it is a built-in function that is used to return a line from an open file.
  • It is used to return a line from a file pointer and stops returning at the specified length, at the end of the file (EOF) or on a new line, at whichever comes first.
  • The file to read and the number of bytes to read are sent as parameters to fgets(), and it returns a -1 byte string from the file specified by the user.
  • 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. 
Returned value:Returns a -1 byte string from a file specified by the user, or False on error.Errors and Exceptions
  • This function is not optimized for large files as it reads one line at a time and a long file can take a long time to fully read.
  • The buffer must be cleared if fgets() is used multiple times.
  • The fgets() function returns Boolean False, but it often happens that it returns a non-boolean value that 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
      // the file is opened using the fopen() function $my_file = fopen ( "gfg.txt" , "rw" );  
    // Prints one line from the open file pointer echo fgets ( $my_file );  
    // file is closed with fclose() fclose ( $my_file ); ?>
    Output:
    This is the first line. 
    Program 2
    // the file is opened using the fopen() function $my_file = fopen ( "gfg.txt " , " rw " );  
    // prints one line at a time until the end of the file is reached while (! feof ( $my_file )) { echo fgets ( $my_file ); }  
    // the file is closed using the fclose() function fclose ( $my_file ); ?>
    Output:
    This is the first line. This is the second line. This is the third line. 
    Link:
    http://php.net/ manual / en / function.fgets.php
  • Shop

    Gifts for programmers

    Best laptop for Excel

    $
    Gifts for programmers

    Best laptop for Solidworks

    $399+
    Gifts for programmers

    Best laptop for Roblox

    $399+
    Gifts for programmers

    Best laptop for development

    $499+
    Gifts for programmers

    Best laptop for Cricut Maker

    $299+
    Gifts for programmers

    Best laptop for hacking

    $890
    Gifts for programmers

    Best laptop for Machine Learning

    $699+
    Gifts for programmers

    Raspberry Pi robot kit

    $150

    Latest questions

    PythonStackOverflow

    Common xlabel/ylabel for matplotlib subplots

    1947 answers

    PythonStackOverflow

    Check if one list is a subset of another in Python

    1173 answers

    PythonStackOverflow

    How to specify multiple return types using type-hints

    1002 answers

    PythonStackOverflow

    Printing words vertically in Python

    909 answers

    PythonStackOverflow

    Python Extract words from a given string

    798 answers

    PythonStackOverflow

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    606 answers

    PythonStackOverflow

    Python os.path.join () method

    384 answers

    PythonStackOverflow

    Flake8: Ignore specific warning for entire file

    360 answers


    Wiki

    Python | How to copy data from one Excel sheet to another

    Common xlabel/ylabel for matplotlib subplots

    Check if one list is a subset of another in Python

    How to specify multiple return types using type-hints

    Printing words vertically in Python

    Python Extract words from a given string

    Cyclic redundancy check in Python

    Finding mean, median, mode in Python without libraries

    Python add suffix / add prefix to strings in a list

    Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

    Python - Move item to the end of the list

    Python - Print list vertically