📖 Scenario: You work in a small shop and want to keep track of your products and prices using a CSV file. You will read the CSV file, update prices, and save the changes back to the file.
🎯 Goal: Build a PHP script that reads a CSV file named products.csv, updates the prices by increasing them by 10%, and writes the updated data back to the same CSV file.
📋 What You'll Learn
Read data from a CSV file named products.csv
Store the CSV data in an array
Increase each product's price by 10%
Write the updated data back to products.csv
Use PHP built-in functions for CSV handling
💡 Why This Matters
🌍 Real World
Small shops or simple apps often use CSV files to store product lists and prices because CSV files are easy to read and edit.
💼 Career
Knowing how to read and write CSV files is useful for data import/export tasks, report generation, and simple database alternatives in PHP development.
Progress0 / 4 steps
1
Create the initial CSV file
Create a CSV file named products.csv with these exact lines: Apple,1.00 Banana,0.50 Cherry,2.00
PHP
Hint
Use file_put_contents to create and write to the CSV file.
2
Read the CSV file into an array
Read the CSV file products.csv line by line using fopen and fgetcsv, and store each row as an array inside a variable called products.
PHP
Hint
Use fopen to open the file and fgetcsv inside a while loop to read each row.
3
Increase each product price by 10%
Use a foreach loop with variables index and product to iterate over products. Increase the price (second element) by 10% and update products[index][1] with the new price formatted to 2 decimal places.
PHP
Hint
Convert the price to float, multiply by 1.10, then format back to string with 2 decimals.
4
Write the updated data back to the CSV file
Open products.csv for writing using fopen with mode 'w'. Use a foreach loop to write each product array to the file using fputcsv. Close the file with fclose. Then print "Updated CSV saved." to confirm.
PHP
Hint
Use fopen with 'w' mode, fputcsv inside a loop, then fclose. Finally, print the confirmation message.
Practice
(1/5)
1. Which PHP function is used to read a line from a CSV file and parse it into an array?
easy
A. fgetcsv()
B. fputcsv()
C. fread()
D. fwrite()
Solution
Step 1: Understand CSV reading functions
PHP provides fgetcsv() to read a line from a CSV file and convert it into an array of values.
Step 2: Differentiate from writing functions
fputcsv() writes arrays to CSV files, while fread() and fwrite() handle raw file reading/writing without CSV parsing.
Final Answer:
fgetcsv() -> Option A
Quick Check:
Read CSV line as array = fgetcsv() [OK]
Hint: Use fgetcsv() to read CSV lines into arrays [OK]
Common Mistakes:
Confusing fgetcsv() with fputcsv()
Using fread() which reads raw data, not CSV parsed
Trying to read CSV without parsing into arrays
2. Which of the following is the correct way to open a CSV file for writing in PHP?
easy
A. $file = fopen('data.csv', 'r');
B. $file = fopen('data.csv', 'rw');
C. $file = fopen('data.csv', 'w');
D. $file = fopen('data.csv', 'x');
Solution
Step 1: Understand fopen modes
Mode 'w' opens a file for writing and truncates it if it exists, perfect for writing CSV files.
Step 2: Check other modes
'r' is read-only, 'rw' is invalid, 'x' creates a new file but fails if it exists.
Final Answer:
$file = fopen('data.csv', 'w'); -> Option C
Quick Check:
Open file for writing = 'w' mode [OK]
Hint: Use 'w' mode in fopen() to write files [OK]
Common Mistakes:
Using 'r' mode when intending to write
Using invalid mode 'rw'
Using 'x' which fails if file exists
3. What will be the output of the following PHP code snippet?
The file is opened with 'r' (read) mode, which does not allow writing.
Step 2: Understand fputcsv requirements
fputcsv() requires the file to be opened in write ('w') or append ('a') mode to write data.
Final Answer:
File opened in read mode instead of write mode -> Option D
Quick Check:
Open file in write mode before writing [OK]
Hint: Open file with 'w' or 'a' before writing CSV [OK]
Common Mistakes:
Opening file in 'r' mode when writing
Forgetting to close the file
Misunderstanding fputcsv() input
5. You want to read a CSV file and write only rows where the third column is greater than 50 to a new CSV file. Which code snippet correctly implements this?
hard
A. $in = fopen('input.csv', 'w');
$out = fopen('output.csv', 'r');
while (($row = fgetcsv($in)) !== false) {
if ($row[2] > 50) {
fputcsv($out, $row);
}
}
fclose($in);
fclose($out);
B. $in = fopen('input.csv', 'r');
$out = fopen('output.csv', 'w');
while (($row = fgetcsv($in)) !== false) {
if ($row[2] > 50) {
fputcsv($out, $row);
}
}
fclose($in);
fclose($out);
C. $in = fopen('input.csv', 'r');
$out = fopen('output.csv', 'w');
while (($row = fgetcsv($out)) !== false) {
if ($row[2] > 50) {
fputcsv($in, $row);
}
}
fclose($in);
fclose($out);
D. $in = fopen('input.csv', 'r');
$out = fopen('output.csv', 'w');
while (($row = fgets($in)) !== false) {
if ($row[2] > 50) {
fputcsv($out, $row);
}
}
fclose($in);
fclose($out);
Solution
Step 1: Open input file for reading and output file for writing
Use 'r' mode for reading input and 'w' mode for writing output.
Step 2: Read each CSV row with fgetcsv() from input and check third column
Use fgetcsv($in) to read rows, then check if $row[2] > 50.
Step 3: Write qualifying rows to output with fputcsv()
If condition passes, write row to output file using fputcsv($out, $row).
Final Answer:
Code snippet B correctly reads, filters, and writes CSV rows -> Option B
Quick Check:
Read with fgetcsv(), filter by column, write with fputcsv() [OK]
Hint: Read with fgetcsv(), filter rows, write with fputcsv() [OK]
Common Mistakes:
Opening files with wrong modes
Using fgets() instead of fgetcsv() for CSV reading
Mixing input and output file handles
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.