0

I am trying to generate a CSV file containing the name and email address from an form in an HTML file and using the PHP fputcsv function. But I am unable to pass the value of variables to the array and it is returning error message.

My PHP file:

<?php 
$name = $_GET["name"];
$email = $_GET["email"];

$list = array
(
'$name', '$email',
);

$file = fopen("list.csv","w");

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

fclose($file);
?>

The error message:

Deprecated: Function split() is deprecated in F:\xampp\htdocs\test.php on line 22

Deprecated: Function split() is deprecated in F:\xampp\htdocs\test.php on line 22

My line 22 contains:

fputcsv($file,split(',',$line));

I don't get the above error if I just use plain text in array. What am I doing wrong?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Gigacore
  • 165
  • 1
  • 2
  • 11
  • 3
    instead of split() there is [explode()](http://php.net/manual/de/function.explode.php) now – Felix Aug 27 '12 at 17:21
  • 1
    What Felix is saying, is that if you look up deprecated, it means that PHP no longer supports the function split. It has been recoded and renamed to explode. Look up the docs for it, and you will have same functionality. – thatidiotguy Aug 27 '12 at 17:23
  • Thanks, now I am not getting any errors, but instead of actual data, the CSV file is being saved with the text $name and $email. How do I pass the variable value in array? – Gigacore Aug 27 '12 at 17:25

3 Answers3

0

single quotes don't replace your variables with their values in strings.

you can either use double quotes

$list = array
(
"$name", "$email",
);

and since these strings only contain your variables, just skip the quotes all together

$list = array
(
$name, $email,
);

for further documentation, see the docs

Felix
  • 812
  • 5
  • 11
  • Also, the values are getting saved in rows instead of two separate columns when opened in spreadsheet. How do I save the name and email in two separate columns? And what I noticed is that there is no comma delimit when opened in notepad. – Gigacore Aug 27 '12 at 17:40
  • again, the documentation ([fputcsv](http://at2.php.net/manual/en/function.fputcsv.php)) is your friend, see the `delimiter` parameter – Felix Aug 27 '12 at 17:45
  • Thank you. I just had to open and close double quotes around both the variables in the array :) – Gigacore Aug 27 '12 at 17:52
0

The error message tells you what is wrong: the function split() is deprecated, meaning it is obsolete (old, will be deactived in future updates of PHP) and PHP warns you against its use.

PHP's online manual for split() helps you to find other functions to achieve what you want, for example explode() or preg_split().

MiDo
  • 1,057
  • 1
  • 7
  • 11
0

As mentioned in your error and in the php manual for that function at http://php.net/manual/en/function.split.php

This function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.

Deprecated => This function is no longer available for your PHP version.

As @Felix suggested, use explode() instead of split(), read about explode: http://php.net/manual/en/function.explode.php

So , instead of:

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

Do:

foreach ($list as $line)
  {
  fputcsv($file,explode(',',$line));
  }
Ofir Baruch
  • 10,323
  • 2
  • 26
  • 39