Page 1 of 1

Endless Loop Issue

Posted: Sat Jun 24, 2006 7:36 am
by Bigun
I attempt to upload and verify an MP3...

Then the loop below never ends

Code: Select all

//Music Uploading goes here
        //Setup loop to check variables
        if ($makechanges == "yes" && $band == "yes") {
                for($i = 0; $i < 10 || $makechanges == "yes"; $i++) {
                        echo $i;
                        $currsongfile = 'songfile' . $i;
                        $currsongname = 'songname' . $i;
                        if ($$currsongname != "" && $$currsongfile != "") {
                                //Song Present - Run Tests
                                // include getID3() library (can be in a different directory if full path is specified)
                                require_once('./getid3/getid3/getid3.php');

                                // Initialize getID3 engine
                                $getID3 = new getID3;

                                // Analyze file and store returned data in $ThisFileInfo
                                $ThisFileInfo = $getID3->analyze($$currsongfile);

                                // Optional: copies data from all subarrays of [tags] into [comments] so
                                // metadata is all available in one location for all tag formats
                                // metainformation is always available under [tags] even if this is not called
                                getid3_lib::CopyTagsToComments($ThisFileInfo);

                                // Output desired information in whatever format you want
                                // Note: all entries in [comments] or [tags] are arrays of strings
                                // See structure.txt for information on what information is available where
                                // or check out the output of /demos/demo.browse.php for a particular file
                                // to see the full detail of what information is returned where in the array
                                $bitrate=@$ThisFileInfo['audio']['bitrate'];           // audio bitrate
                                $time=@$ThisFileInfo['playtime_string'];            // playtime in minutes:seconds, formatted string
                                $format=@$ThisFileInfo['fileformat'];
                                if ($bitrate == "" || $time == "" || $format != "mp3") {
                                        //The file has no valid bitrate or no valid time or isn't an MP3 - reject
                                        $makechages = "no";
                                        echo "<center><font color=red><b>The File uploaded doesn't seem to be a valid MP3 - Rejecting File</b></font></center><br>";
                                }
                        }
                }
}
I'm fairly sure it's this line here:

Code: Select all

for($i = 0; $i < 10 || $makechanges == "yes"; $i++) {
But how else would I exit the loop if an invalid MP3 is found?

Posted: Sat Jun 24, 2006 7:44 am
by Bigun
I see the problem... it should be &&(and) not ||(or).

Posted: Sat Jun 24, 2006 2:41 pm
by sheila
You've also got a typo. This

Code: Select all

$makechages = "no";
should be

Code: Select all

$makechanges = "no";

Posted: Sat Jun 24, 2006 2:48 pm
by phpCCore Brad
Does the || really make a difference? Aren't both of them valid in PHP?

http://us3.php.net/manual/en/language.o ... ogical.php

Is it possible the script is looping forever for another reason. Very strange that a for next would look forever anyhow, since it has a definite ending point... I mean I could be completely wrong. I always use and and or. Not && and ||