Free cookie consent management tool by TermsFeed Generator PHP CSV Files | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP CSV Files

PHP CSV Files

Last Updated on Mar 21, 2023

Intro

Do you remember working with files? Then let's get started

Read CSV

reading csv is like reading a file but instead of reading the line with fgets we use fgetcsv to read the line

fgetcsv(file, length, separator, enclosure);

File: is the file we have opened

Length: 0 for reading the whole row or you can limit how much of the row it should read

Separator: for csv file the default is comma “,”

Enclosure: for quotes the default is double quotes ‘“‘

/* the csv file looks like this
id,name
1,Amir
2,Pratham
3,Simon
*/
$file = fopen('example.csv','r');
//fgetcsv(file, length, separator, enclosure, escape);
$row = fgetcsv($file);
while($row != false){
    echo $row[0] . '-' . $row[1];
    echo '<br>';
    $row = fgetcsv($file);
}
fclose($file);
/* output is :
id-name
1-Amir
2-Pratham
3-Simon
 */

Write CSV

Like writing to a file but instead of using fwrite we use the fputcsv function. The first arg is the file and the second arg is all the columns of that row as an array.

It will take care of the new line and enclosing the string and everything else. Super easy!

$firstRow = ['id','name'];
$names = [
    ['1','Amir'],
    ['2','Pratham'],
    ['3','Simon'],
];
$file = fopen('example.csv','w');
fputcsv($file,$firstRow);
foreach($names as $name){
    fputcsv($file,$name);
}
fclose($file);

Here writing and reading the file at once

// write to a csv
$firstRow = ['id','name'];
$names = [
    ['1','Amir'],
    ['2','Pratham'],
    ['3','Simon'],
];
$file = fopen('example.csv','w');
fputcsv($file,$firstRow);
foreach($names as $name){
    fputcsv($file,$name);
}
fclose($file);
/* the csv file looks like this
id,name
1,Amir
2,Pratham
3,Simon
*/

// read the csv file
$file = fopen('example.csv','r');
//fgetcsv(file, length, separator, enclosure, escape);
$row = fgetcsv($file);
while($row != false){
    echo $row[0] . '-' . $row[1];
    echo '<br>';
    $row = fgetcsv($file);
}
fclose($file);
/* output is :
id-name
1-Amir
2-Pratham
3-Simon
 */

https://youtu.be/_t8RhQRxHVA

Conclusion

Now you know about working with csv files in PHP.

I recommend you to open a PHP files and try write to a csv file and read the data from the same file.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • what are csv files
  • read csv
  • write csv

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

undo git merge the easy way and then undo the undoing! (video)
Nov 27, 2022 programming

undo git merge the easy way and then undo the undoing! (video)

we are going to talk about how to undo git merge and then we talk about how to undo the undoing ...

9 Min Read Read More
PHP Operators
Mar 21, 2023 programming

PHP Operators

Today we are going to learn about Operators in PHP including arithmetic, comparison and logic operators. You will be using all of these operators during your career a lot. ...

5 Min Read Read More
PHP Array Intersect
Feb 14, 2023 programming

PHP Array Intersect

Today we are going to talk about array intersect functions in PHP .If you want to find the items in your array that are present in the other arrays, array intersect is your friend. ...

5 Min Read Read More
Top Open Source Licenses: Understanding Your Options
May 07, 2024 programming

Top Open Source Licenses: Understanding Your Options

Open source licenses dictate how software can be used, modified, and distributed. Understanding the different types of licenses is essential for developer. ...

9 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...