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', '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) { $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.
Reference: http://php.net/manual/en/splfileobject.fputcsv.php
Recommended Posts:
- PHP | fputcsv( ) Function
- PHP | SplFileObject eof() Function
- PHP | SplFileObject fread() Function
- PHP | SplFileObject fgetc() Function
- PHP | SplFileObject current( ) Function
- PHP | SplFileObject seek() Function
- PHP | SplFileObject fstat() Function
- PHP | SplFileObject ftruncate() Function
- PHP | SplFileObject fwrite() Function
- PHP | SplFileObject getMaxLineLen() Function
- PHP | SplFileObject flock() Function
- PHP | SplFileObject setMaxLineLen() Function
- PHP | SplFileObject ftell() Function
- PHP | SplFileObject rewind() Function
- PHP | SplFileObject fgetss() Function
- PHP | SplFileObject fgets() Function
- How to call a function that return another function in JavaScript ?
- How to Check a Function is a Generator Function or not using JavaScript ?
- How to get the function name from within that function using JavaScript ?
- How to get the function name inside a function in PHP ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


