More Loop Confusion

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
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

More Loop Confusion

Post 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.
Last edited by Bigun on Sun Jun 25, 2006 7:54 pm, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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);
    }
}
?>
Last edited by RobertGonzalez on Sun Jun 25, 2006 7:54 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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] != "")
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

Post 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++) {
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

Post 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)
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I don't know what you're trying to do so I can't tell you what to do.
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

Post 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 "".
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

== and != are typeless comparisons.. therefore 0, null, false, and "" are all equals. If you want type strict, it's === and !==.
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

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