0
0
PHPprogramming~20 mins

CSV file reading and writing in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CSV Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this PHP code reading a CSV file?

Consider the following PHP code that reads a CSV file and prints each row as a JSON string.

$file = fopen('data.csv', 'r');
while (($row = fgetcsv($file)) !== false) {
    echo json_encode($row) . "\n";
}
fclose($file);

Assuming data.csv contains these lines:

apple,banana,cherry
1,2,3
x,y,z

What will be the output?

PHP
$file = fopen('data.csv', 'r');
while (($row = fgetcsv($file)) !== false) {
    echo json_encode($row) . "\n";
}
fclose($file);
A
["apple","banana","cherry"]
["1","2","3"]
["x","y","z"]
B
Error: Cannot open file
C
["apple,banana,cherry"]
["1,2,3"]
["x,y,z"]
D
apple,banana,cherry
1,2,3
x,y,z
Attempts:
2 left
💡 Hint

Remember that fgetcsv returns an array of fields for each line.

Predict Output
intermediate
2:00remaining
What does this PHP code write to a CSV file?

Given this PHP code that writes data to a CSV file:

$data = [
    ['Name', 'Age', 'City'],
    ['Alice', 30, 'New York'],
    ['Bob', 25, 'Los Angeles']
];
$file = fopen('output.csv', 'w');
foreach ($data as $row) {
    fputcsv($file, $row);
}
fclose($file);

What will be the content of output.csv after running this code?

PHP
$data = [
    ['Name', 'Age', 'City'],
    ['Alice', 30, 'New York'],
    ['Bob', 25, 'Los Angeles']
];
$file = fopen('output.csv', 'w');
foreach ($data as $row) {
    fputcsv($file, $row);
}
fclose($file);
A
Name,Age,City
Alice,30,New York
Bob,25,Los Angeles
B
["Name","Age","City"]
["Alice",30,"New York"]
["Bob",25,"Los Angeles"]
C
Name;Age;City
Alice;30;New York
Bob;25;Los Angeles
D
Error: Cannot write to file
Attempts:
2 left
💡 Hint

fputcsv writes arrays as comma-separated lines by default.

🔧 Debug
advanced
2:00remaining
Why does this PHP code reading CSV raise a warning?

Look at this PHP code snippet:

$file = fopen('data.csv', 'r');
while ($row = fgetcsv($file)) {
    print_r($row);
}
fclose($file);

Sometimes it raises a warning: Warning: fgetcsv() expects parameter 1 to be resource, bool given. Why?

PHP
$file = fopen('data.csv', 'r');
while ($row = fgetcsv($file)) {
    print_r($row);
}
fclose($file);
ABecause fgetcsv requires a string filename, not a resource.
BBecause print_r cannot print arrays returned by fgetcsv.
CBecause fopen failed and returned false, so fgetcsv got a boolean instead of a file resource.
DBecause fclose was called before the loop started.
Attempts:
2 left
💡 Hint

Check the return value of fopen before using it.

📝 Syntax
advanced
2:00remaining
Which option correctly reads a CSV file into an array of rows?

Which PHP code snippet correctly reads all rows from a CSV file data.csv into an array $rows?

A
$rows = [];
$file = fopen('data.csv', 'r');
while ($row = fgetcsv($file)) {
    $rows[] = $row;
}
fclose($file);
B
$rows = [];
$file = fopen('data.csv', 'r');
while (($row = fgetcsv($file)) !== false) {
    $rows[] = $row;
}
fclose($file);
C
$rows = [];
$file = fopen('data.csv', 'r');
while ($row = fgetcsv($file) !== false) {
    $rows[] = $row;
}
fclose($file);
D
$rows = [];
$file = fopen('data.csv', 'r');
while ($row !== false = fgetcsv($file)) {
    $rows[] = $row;
}
fclose($file);
Attempts:
2 left
💡 Hint

Remember operator precedence and correct syntax for while loops.

🚀 Application
expert
2:00remaining
How to write a CSV file with semicolon delimiter in PHP?

You want to write a CSV file data.csv where fields are separated by semicolons (;) instead of commas. Which code snippet correctly does this?

A
$file = fopen('data.csv', 'w');
fputcsv($file, ['a', 'b', 'c']);
fclose($file);
B
$file = fopen('data.csv', 'w');
fputcsv($file, ['a', 'b', 'c'], ',');
fclose($file);
C
$file = fopen('data.csv', 'w');
fwrite($file, 'a;b;c\n');
fclose($file);
D
$file = fopen('data.csv', 'w');
fputcsv($file, ['a', 'b', 'c'], ';');
fclose($file);
Attempts:
2 left
💡 Hint

Check the third parameter of fputcsv for delimiter.