If the content of a file is long, the performance of fgets() will be poor. Let’s see this example:
$ts = microtime(true);
$fh = fopen(‘input.in’, ‘r’);
for ($i = 0; $i < 100000; $i++) fgets($fh, 1000000); printf("%.4f\n", microtime(true) - $ts);
Result:
8.4704
To improve the performance, we can use file_get_contents() instead.
$ts = microtime(true);
$lines = explode(“\n”, file_get_contents(‘input.in’));
printf(“%.4f\n”, microtime(true) – $ts);
Result:
0.0495