what does $line mean in this piece of code?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
abrogard
Forum Newbie
Posts: 11
Joined: Sun Apr 04, 2004 6:02 am

what does $line mean in this piece of code?

Post 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]
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
Post Reply