Page 1 of 1

what does $line mean in this piece of code?

Posted: Mon Jul 03, 2006 8:14 am
by abrogard
Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'm a raw beginner trying to learn how to save my form data as a csv file. I've got as far as finding fputcsv() and this piece of code that illustrates its use. 

 But I can't figure out  what the line  "foreach($list as $line)" means.

  Well, I can, it means for each line in the array $list write a csv line in the file using "," as a separator.

 But how does it mean that?  Is there any significance in "$line" or could I write "foreach($list as $foobah)" ?

 (I should test it and find out for myself but I can't right now..  not on this machine... when I get home)





Using fputcsv()

Code: Select all

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

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

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

fclose($file);
?>

Pimptastic | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Jul 03, 2006 8:20 am
by CoderGoblin

Code: Select all

[url]http://de2.php.net/manual/en/control-structures.foreach.php]foreach[/url] ($list as $line)
{
fputcsv($file,split(',',$line));
}
$list is an array variable. The foreach steps through puuting each value as $line. This is the used within the loop. The $line could be changed to $foobar if the fputcsv is also change to $foobar.

It should be noted that the $list is a copy. if you change $line via e.g $line='Does this change'; $list itself is not changed.