Jump to content

foreach loop


akshay

Recommended Posts

Hello.

 

I need help regarding foreach loop.

 

php.net manual says, foreach means, FOR each element of AN ARRAY, do the following...

 

But, can someone explain me the logic used in these code snipplets (only for foreach statements) :

 

PHP Upload Script:

 

<?php

$file_dir="d:/wamp/www/test/files";

foreach($_FILES as $file_name => $file_array) {  //what does this mean? ONLY the foreach statement
print "Path: ".$file_array['tmp_name']."<br>";
print "Name: ".$file_array['name']."<br>";
print "Type: ".$file_array['type']."<br>";
print "Size: ".$file_array['size']."<br>";
$flnm=$file_array['name'];
if (is_uploaded_file($file_array['tmp_name'])) {
move_uploaded_file($file_array['tmp_name'],
"$file_dir/$flnm") or die("Error!");
print "Upload Successful!";
}
}

?>

 

 

and another one is simpler (printing date values using php getdate() function) :

 

$date_array=getdate();
foreach($date_array as $key=>$val) { //explain this
print "$key = $val<BR>";
}

// I simply use this code:
print "<BR><BR>The Date is:<br>";
print $date_array['mday']." ";
print $date_array['month']." ";
print $date_array['weekday']. " ";
print $date_array['year']." ";

 

 

Link to comment
https://forums.phpfreaks.com/topic/193047-foreach-loop/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.