0
0
PHPprogramming~5 mins

CSV file reading and writing in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What PHP function is commonly used to read a CSV file line by line?
The fgetcsv() function reads a line from an open file and parses it as CSV, returning an array of fields.
Click to reveal answer
beginner
How do you open a file for writing CSV data in PHP?
Use fopen() with the mode 'w' to open a file for writing. This will create the file if it doesn't exist or overwrite it if it does.
Click to reveal answer
beginner
Which PHP function writes an array as a CSV line to a file?
The fputcsv() function takes an array and writes it as a CSV formatted line to an open file.
Click to reveal answer
beginner
What is the purpose of closing a file after reading or writing CSV data?
Closing a file with fclose() frees system resources and ensures all data is properly saved and no corruption occurs.
Click to reveal answer
intermediate
How can you handle CSV files with different delimiters in PHP?
Both fgetcsv() and fputcsv() accept an optional delimiter parameter to specify characters like semicolon ; instead of comma ,.
Click to reveal answer
Which function reads a CSV line into an array in PHP?
Afgetcsv()
Bfputcsv()
Cfread()
Dfile_get_contents()
What mode should you use with fopen() to write a new CSV file?
A'w'
B'r'
C'a'
D'x'
Which function writes an array as a CSV line to a file?
Afwrite()
Bfgetcsv()
Cfputcsv()
Dfile_put_contents()
Why should you always close a file after reading or writing CSV data?
ATo delete the file
BTo free system resources and save data properly
CTo open it again automatically
DTo convert it to JSON
How can you read a CSV file that uses semicolons instead of commas?
AUse <code>file_get_contents()</code> without changes
BUse <code>fread()</code> only
CReplace semicolons manually after reading
DUse <code>fgetcsv()</code> with the delimiter parameter set to ';'
Explain the steps to read a CSV file line by line in PHP and process each row.
Think about opening, reading, looping, and closing the file.
You got /4 concepts.
    Describe how to write multiple rows of data to a CSV file in PHP.
    Remember to open, write each row, then close.
    You got /3 concepts.