Skip to content
Related Articles

Related Articles

PHP | SplFileObject fputcsv() Function

Improve Article
Save Article
  • Last Updated : 21 Dec, 2018
Improve Article
Save Article

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.


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 :

// Create an Array
$gfg = array (
    array('gfg', 'geeks', '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) {
echo "Successfully write data in gfg.csv";


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.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!