for loop not showing my array

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
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

for loop not showing my array

Post by psychotomus »

Code: Select all

$datas = array();
if ($handle = opendir("downloads/")) 
{
 
    while (($file = readdir($handle)) !== false) 
    {
        $datas[] = $file;
    }
    
    closedir($handle);
}
 
echo count(datas); //displays 18
for($i=0; $i<count(datas); $i++)
{   echo $datas[$i];
}
User avatar
N1gel
Forum Commoner
Posts: 95
Joined: Sun Apr 30, 2006 12:01 pm

Re: for loop not showing my array

Post by N1gel »

Try this to print out your array and see what data you have

Code: Select all

print_r($datas)
or this to check that your for loop is being executed

Code: Select all

for($i=0; $i<count(datas); $i++)
{  
echo "<p>$i -".$datas[$i]."</P>";
}
 
xtiano77
Forum Commoner
Posts: 72
Joined: Tue Sep 22, 2009 10:53 am
Location: Texas

Re: for loop not showing my array

Post by xtiano77 »

Code: Select all

 
[color=#FF0000]$[/color]datas = array();
echo count([color=#FF0000]$[/color]datas); //displays 18
for($i=0; $i<count([color=#FF0000]$[/color]datas); $i++)
{   echo $datas[$i];
}
 
I believe you declared the array as $datas, but later referred to it as "datas" omitting the "$". I think that if you put in the "$" it will fix the problem.

Just my two cents.
Post Reply