What is the function of fgets in PHP and how can it be used to read an entire file?

The function of fgets in PHP is to read a single line from a file. To read an entire file using fgets, you can use a loop to read each line until the end of the file is reached.

$file = fopen("file.txt", "r");

if ($file) {
    while (!feof($file)) {
        $line = fgets($file);
        echo $line;
    }
    fclose($file);
} else {
    echo "Error opening file.";
}