Page 1 of 1

More Loop Confusion

Posted: Sun Jun 25, 2006 7:35 pm
by Bigun
The code below will not execute...

Code: Select all

for($i=0; $i < 10 && $deletefilelist[$i] != ""; $i++) {
                        echo "hello";
                        $filetodelete=$deletefilelist[$i];
                        $deletesid=mysql_result($songdata,$filetodelete,"sid");
                        $deletefile=mysql_result($songdata,$filetodelete,"file");
                        $deletedir="./bands/songs/".$uid."/";
                        //Delete File
                        unlink($deletedir."/".$deletefile);
                        echo $deletedir."/".$deletefile."<br>";
                        //Remove from Song Database
                        $query="DELETE FROM `songs` WHERE `sid` = '".$deletesid."' LIMIT 1;";
                        echo $query;
                        mysql_query($query);
                }
The 'hello' portion of the code doesn't show..

Yet an echo of $deletefilelist[0] right before the loop echo's a zero.

Posted: Sun Jun 25, 2006 7:54 pm
by RobertGonzalez
Do your comparison inside the loop...

Code: Select all

<?php
for($i=0; $i < 10; $i++) {
    if (!empty($deletefilelist[$i]))
    {
        echo "hello";
        $filetodelete=$deletefilelist[$i];
        $deletesid=mysql_result($songdata,$filetodelete,"sid");
        $deletefile=mysql_result($songdata,$filetodelete,"file");
        $deletedir="./bands/songs/".$uid."/";
        //Delete File
        unlink($uploaddir."/".$deletefile);
        echo $uploaddir."/".$deletefile."<br>";
        //Remove from Song Database
        $query="DELETE FROM `songs` WHERE `sid` = '".$deletesid."' LIMIT 1;";
        echo $query;
        mysql_query($query);
    }
}
?>

Posted: Sun Jun 25, 2006 7:54 pm
by feyd
I think the logic surrounding the $deletefilelist comparison would be the problem.

If what you say about echoing $deletefilelist[0] does, in fact, echo a zero, your comparison will short circuit the loop. To check out what I mean, var_dump($deletefilelist[0] != "")

Posted: Sun Jun 25, 2006 7:56 pm
by Bigun
But why doesn't the '$i' value get inserted into the conditional portion of the loop?

Code: Select all

for($i=0; $i < 10 && $deletefilelist[$i] != ""; $i++) {

Posted: Sun Jun 25, 2006 7:58 pm
by Bigun
feyd wrote:I think the logic surrounding the $deletefilelist comparison would be the problem.

If what you say about echoing $deletefilelist[0] does, in fact, echo a zero, your comparison will short circuit the loop. To check out what I mean, var_dump($deletefilelist[0] != "")

Code: Select all

0bool(false)

Posted: Sun Jun 25, 2006 8:01 pm
by Bigun
feyd wrote:I think the logic surrounding the $deletefilelist comparison would be the problem.

If what you say about echoing $deletefilelist[0] does, in fact, echo a zero, your comparison will short circuit the loop. To check out what I mean, var_dump($deletefilelist[0] != "")
How would you redo the comparison then?

Posted: Sun Jun 25, 2006 8:04 pm
by feyd
I don't know what you're trying to do so I can't tell you what to do.

Posted: Sun Jun 25, 2006 8:10 pm
by Bigun
feyd wrote:I don't know what you're trying to do so I can't tell you what to do.
Simply put there is a difference between:

Code: Select all

$x = 0
and

Code: Select all

$x = ""
That number correlates to a number on a mysql database that would need to be deleted. But if there is no number, just skip the loop altogether.

It more or less just bugs me that the loop is telling me that 0 is the same as "".

Posted: Sun Jun 25, 2006 8:13 pm
by feyd
== and != are typeless comparisons.. therefore 0, null, false, and "" are all equals. If you want type strict, it's === and !==.

Posted: Sun Jun 25, 2006 8:21 pm
by Bigun
feyd wrote:== and != are typeless comparisons.. therefore 0, null, false, and "" are all equals. If you want type strict, it's === and !==.
Bingo...

Learned yet another oddity in code.