Is it advisable to use fread() or fgets() when reading from a file in PHP?

When reading from a file in PHP, it is generally advisable to use fgets() over fread(). fgets() reads a line from the file pointer, making it easier to process text files line by line. This can be more memory efficient and easier to work with than fread(), which reads a specific number of bytes from the file.

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

// Using fgets to read file line by line
while(!feof($file)) {
  $line = fgets($file);
  echo $line;
}

fclose($file);