Sunday, October 5, 2025
HomeLanguagesPHP | SplFileObject fputcsv() Function

PHP | SplFileObject fputcsv() Function

The SplFileObject fputcsv() function is an inbuilt function of Standard PHP Library (SPL) in PHP which is used write a field array as a CSV line.

Syntax:

string SplFileObject::fputcsv()

Parameters: This function accept four parameters one is mandatory and three are optional.

  • $fields: Specifies the an array of values.
  • $delimiter: An optional parameter which specify sets the field delimiter.
  • $enclosure: An optional parameter which specify field enclosure.
  • $escape: An optional parameter used for escape character.

Return values: This function returns length of the written string or FALSE otherwise.

Below Program illustrate the SplFileObject fputcsv() function in PHP.

Program :




<?php
  
// Create an Array
$gfg = array (
    array('gfg', 'neveropen', 'gced', 'Article'),
    array('Hello', 'Sudo', 'Placement'),
    array('"Contribute"', '"Interview"'),
    array('"System"', '"IDE"')
);
  
// Creating Spl Object
$file = new SplFileObject('gfg.csv', 'w');
  
foreach ($gfg as $arr) {
    $file->fputcsv($arr);
}
  
  
echo "Successfully write data in gfg.csv";
?>


Output:

Successfully write data in gfg.csv

When Run the Above program it will create a gfg.csv file if not exist and writes the content of array in file as shown in below image.

https://geeksforgeeks.org/wp-content/uploads/2023/10/capture1-2-1.png

Reference: http://php.net/manual/en/splfileobject.fputcsv.php

RELATED ARTICLES

Most Popular

Dominic
32337 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6706 POSTS0 COMMENTS
Nicole Veronica
11871 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6823 POSTS0 COMMENTS
Ted Musemwa
7089 POSTS0 COMMENTS
Thapelo Manthata
6779 POSTS0 COMMENTS
Umr Jansen
6779 POSTS0 COMMENTS