Home  >  Article  >  Backend Development  >  What is the usage of fgets function in php

What is the usage of fgets function in php

WBOY
WBOYOriginal
2021-12-29 16:49:532872browse

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)".

What is the usage of fgets function in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn