Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
3  
instead of split() there is explode() 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? –  Santhosh Sundar Aug 27 '12 at 17:25

3 Answers 3

up vote 0 down vote accepted

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

share|improve this answer
    
Perfect. That worked. Thank you! –  Santhosh Sundar Aug 27 '12 at 17:36
    
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. –  Santhosh Sundar Aug 27 '12 at 17:40
    
again, the documentation (fputcsv) 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 :) –  Santhosh Sundar Aug 27 '12 at 17:52

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));
  }
share|improve this answer
    
Thanks you very much :) –  Santhosh Sundar Aug 27 '12 at 17:59

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

share|improve this answer
    
Thank you, that helped! –  Santhosh Sundar Aug 27 '12 at 17:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.