PHP fputcsv() Function
In this chapter you will learn:
- Definition for PHP fputcsv() Function
- Syntax for PHP fputcsv() Function
- Parameter for PHP fputcsv() Function
- Return for PHP fputcsv() Function
- Example - formats a line as CSV and writes it to an open file
Definition
The fputcsv() function formats a line as CSV and writes it to an open file.
Syntax
PHP fputcsv() Function has the following syntax.
fputcsv(file,fields,seperator,enclosure)
Parameter
Parameter | Is Required | Description |
---|---|---|
file | Required. | Open file to write to |
fields | Required. | Specifies which array to get the data from |
separator | Optional. | A character that specifies the field separator. Default is comma ( , ) |
enclosure | Optional. | A character that specifies the field enclosure character. Default is " |
Return
This function returns the length of the written string, or FALSE on failure.
Example
formats a line as CSV and writes it to an open file
<?php// j a va 2 s . c o m
$list = array("A,B,C,D","E,F,G,H",);
$file = fopen("contacts.csv","w");
foreach ($list as $line){
fputcsv($file,split(',',$line));
}
fclose($file);
?>
Next chapter...
What you will learn in the next chapter:
- Definition for PHP fputs() Function
- Syntax for PHP fputs() Function
- Parameter for PHP fputs() Function
- Return for PHP fputs() Function
- Example - Write string to a file
Home » PHP Tutorial » PHP File Functions