In PHP, feof()
Is a function used to detect whether the file pointer has reached the end of the file. Its function is to determine whether the current position of the file pointer is at the end of the file.
feof()
The syntax of is as follows:
bool feof (resource $handle )
$handle
: Required. Indicates the file handle to be detected (usually using fopen()
Function to open a file).
feof()
The function returns a Boolean value. If the file pointer has reached the end of the file, it returns true
; Otherwise, return false
。
The following is an example of using feof()
Examples of functions:
$file = fopen ( "data.txt" , "r" ); if ( $file ) { while (! feof ( $file )) { $line = fgets ( $file ); echo $line ; } fclose ( $file ); } else { echo "Failed to open file." ; }
In the above example, we first use the fopen()
Function opened a file named data.txt
And assign it to a variable $file
。 Then, use the feof()
Function to check whether the file pointer has reached the end of the file.
In each cycle, we use fgets()
The function reads a line of data from a file and assigns it to a variable $line
。 Then, output the row data.
Finally, use the fclose()
Function to close a file handle.
After executing the above code, it will read line by line data.txt
And output the contents of the file.
To sum up, feof()
The function detects whether the file pointer has reached the end of the file. It can be used as the end condition to judge the read operation when processing the file content.