What is the best approach to iterate through a file in PHP and count specific lines that meet a condition?
To iterate through a file in PHP and count specific lines that meet a condition, you can open the file, read it line by line, check if each line meets the condition, and increment a counter accordingly. Once you have gone through all the lines in the file, you will have the count of lines that meet the condition.
$file = fopen("file.txt", "r");
$count = 0;
while(!feof($file)) {
$line = fgets($file);
// Check if the line meets the condition
if (/* condition */) {
$count++;
}
}
fclose($file);
echo "Number of lines that meet the condition: " . $count;