Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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().

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));
  }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top