Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

fputcsv()

Example

Format a line as CSV and writes it to an open file:rephrase

<?php
$list = array (
  array(“Peter”“Griffin” ,“Oslo”“Norway”),
  array(“Glenn”“Quagmire”“Oslo”“Norway”)
);

$file = fopen(“contacts.csv”,“w”);

foreach ($list as $line) {
  fputcsv($file, $line);
}

fclose($file);
?>

Definition and Usage

A line is formatted as CSV and written to an open file using the fputcsv() method.

See the fgetcsv() function as well.

Syntax

fputcsv(filefieldsseparatorenclosureescape)

Parameter Values

 

Parameter

Description

file

Essential. The open file to write to is specified.

fields

Essential. indicates the array from which to retrieve the data.

separator

Not required. The field separator is indicated by this character. By default, rephrase is comma (, )

enclosure

Optional. A character that specifies the field enclosure character. Default is “

escape

Optional. Specifies the escape character. Default is “\\”. Can also be an empty string (“”) which disables the escape mechanism

Technical Details

Return Value:

The written string’s length in the event of success and FALSE in the event of failure

PHP Version:

5.1+

PHP Changelog:

PHP 7.4: To turn off the escape mechanism, you may now pass an empty string as the escape argument.
PHP 5.5: The escape parameter (rephrase) was added.