Home > Article > Backend Development > What is the usage of fgets function in php
In PHP, the fgets function is used to read a line of a specified file and return the reading result. The function will end reading when it reaches the specified length, encounters a newline character, or reads to the end of the file. It fails. The return result is false, and the syntax is "fgets(file,length)".
The operating environment of this tutorial: windows10 system, PHP7.1 version, DELL G3 computer
fgets function in php What is the usage
#fgets() function returns a line from the open file.
fgets() function will stop returning a new line when it reaches the specified length (length - 1), encounters a newline character, or reaches the end of file (EOF) (whichever comes first).
This function returns FALSE if it fails.
The syntax is:
fgets(file,length)
where file is required to specify the file to be read, and length is optional to specify the number of bytes to be read. The default is 1024 bytes.
Examples are as follows:
<?php $file = fopen("test.txt","r"); echo fgets($file); fclose($file); ?>
or
<?php $file = fopen("test.txt","r"); while(! feof($file)) { echo fgets($file). "<br />"; } fclose($file); ?>
If you are interested, you can click on "PHP Video Tutorial" to learn more about PHP knowledge study.
The above is the detailed content of What is the usage of fgets function in php. For more information, please follow other related articles on the PHP Chinese website!