What is the usage of fgets function in php

WBOY
Release: 2023-03-14 16:32:01
Original
2873 people have browsed it

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)
Copy after login

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);
?>
Copy after login

or

<?php
$file = fopen("test.txt","r");
 
while(! feof($file))
{
    echo fgets($file). "<br />";
}
 
fclose($file);
?>
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template