Bird
0
0

You want to read a CSV file and skip empty lines while reading. Which code snippet correctly does this?

hard📝 Application Q8 of 15
PHP - File Handling
You want to read a CSV file and skip empty lines while reading. Which code snippet correctly does this?
A$file = fopen('data.csv', 'r'); while ($row = fgets($file)) { if ($row != '') { print_r($row); } } fclose($file);
B$file = fopen('data.csv', 'r'); while (($row = fgetcsv($file)) !== null) { print_r($row); } fclose($file);
C$file = fopen('data.csv', 'r'); while (($row = fgetcsv($file)) !== false) { if (!empty(array_filter($row))) { print_r($row); } } fclose($file);
D$file = fopen('data.csv', 'r'); while (($row = fgetcsv($file)) !== false) { print_r($row); } fclose($file);
Step-by-Step Solution
Solution:
  1. Step 1: Use fgetcsv() to read CSV lines

    fgetcsv() returns false on EOF, so loop condition is correct.
  2. Step 2: Skip empty lines by checking if row has any non-empty values

    array_filter removes empty elements; !empty() ensures line is not empty.
  3. Final Answer:

    Code snippet B correctly skips empty CSV lines -> Option C
  4. Quick Check:

    Skip empty CSV lines = check array_filter result [OK]
Quick Trick: Use array_filter() to detect non-empty CSV rows [OK]
Common Mistakes:
  • Using fgets() which returns string
  • Checking !== null instead of !== false
  • Not filtering empty rows

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes