Home > Article > Backend Development > What does fgets mean in php
In PHP, fgets means "reading a line of data from a file"; the fgets() function can read one line of data at a time from an open file and return data of a specified length. The syntax "fgets( $handle,$length)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
PHP fgets is a programming language developed based on PHP language A function that can read a row of data from a file pointer and return the data.
fgets() function is used to read one line of data at a time from an open file. The syntax format of this function is as follows:
fgets($handle,$length)
The parameter $handle
is to be opened file; parameter $length
is an optional parameter, used to set the length of data to be read. The
function can read a line from the specified file $handle
and return a string with a maximum length of $length
-1 bytes. Stops after encountering a newline character, EOF, or reading $length-1
bytes. If the $length
parameter is omitted, the default read length is 1k (1024 bytes).
Example:
Use the fgets() function to read a line of data from the "test.txt" file
<?php $file = fopen("test.txt","r"); echo fgets($file); fclose($file); ?>
Then the loop statement can also read all the data line by line:
<?php $handle = fopen("test.txt","r"); if ($handle) { while (($info = fgets($handle, 1024)) !== false) { echo $info.'<br>'; } fclose($handle); } ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What does fgets mean in php. For more information, please follow other related articles on the PHP Chinese website!