Google News
logo
PHP Program to Use fgets() to read a single line from a file
To read a single line from a file using PHP, we can use the `fgets()` function. The `fgets()` function reads a single line from the file and returns it as a string. Here's an example program that demonstrates how to use the `fgets()` function :
Program :
<?php
$file = fopen("example.txt", "r") or die("Unable to open file!"); // open the file in read mode

$line = fgets($file); // read a single line from the file

echo $line; // display the line

fclose($file); // close the file
?>
In the above program, we opened the file "example.txt" in read mode using the `fopen()` function. Then we read a single line from the file using the `fgets()` function and stored it in the variable `$line`.

Finally, we displayed the line using `echo` statement and closed the file using the `fclose()` function.