map200uk Posted May 29, 2007 Share Posted May 29, 2007 hi, im having trouble with part of my code, trying to use a switch inside a foreach i just cant get it to work foreach ($test as $value) { echo "<br><br><b>MP3 found => \n $value</b>"; // $i=$i+1; // echo "<br> \n $i"; echo "abc"; // echo "<br> new path is \n $path"; switch($value) { case strstr($value,"mp3"): echo "<br> mp3 found \n $value"; break; case strstr($value,"ogg"): echo "<br>ogg found \n $value"; break; } } if i do if strstr it works tho?! could someone tell me if the code is wrong or what, ive looked on php.net manual and my code looks ok> thanks Quote Link to comment https://forums.phpfreaks.com/topic/53456-noob-error-i-think/ Share on other sites More sharing options...
per1os Posted May 29, 2007 Share Posted May 29, 2007 Why use a case for that, that is a part where an IF statement might be better: <?php foreach ($test as $value) { echo "<br><br><b>MP3 found => \n $value</b>"; // $i=$i+1; // echo "<br> \n $i"; echo "abc"; // echo "<br> new path is \n $path"; if (strstr($value,"mp3")) { echo "<br> mp3 found \n $value"; }elseif (strstr($value,"ogg")) { echo "<br>ogg found \n $value"; } } ?> The reasoning the case did not work is because you were using $value in the case. How case statements work is that it checks the case against the $value. So if $value = "mp3" and the case 'mp3': it will enter that case. So if the value was 'yourfile.mp3' and you do a strstr($value, 'mp3'); this just returns a position at where mp3 is which would be 8 and 8 does not equal what $value is does it? Hope that makes sense. Quote Link to comment https://forums.phpfreaks.com/topic/53456-noob-error-i-think/#findComment-264185 Share on other sites More sharing options...
map200uk Posted May 29, 2007 Author Share Posted May 29, 2007 what you say makes perfect sense, thanks! but, 'string strstr ( string $haystack, string $needle ) Returns part of haystack string from the first occurrence of needle to the end of haystack. ' wouldnt it mean it returns 'mp3' then? sorry if im being dumb=] Quote Link to comment https://forums.phpfreaks.com/topic/53456-noob-error-i-think/#findComment-264188 Share on other sites More sharing options...
per1os Posted May 29, 2007 Share Posted May 29, 2007 Seems to be true. I actually never knew that, crazy stuff. Anyhow I am sure it had something to do with the case and the strstr. But still what you are trying to do is check a part of $value against $value. Make sense? Beacuse $value = "filename.mp3" strstr($value, "mp3") which returns "mp3" does not equal $value right? Switch is really not based on true/false conditions like if is. Switch checks on variable vs another if those variables are equal it enters into that case. Quote Link to comment https://forums.phpfreaks.com/topic/53456-noob-error-i-think/#findComment-264191 Share on other sites More sharing options...
map200uk Posted May 29, 2007 Author Share Posted May 29, 2007 i think so:) thanks mate Quote Link to comment https://forums.phpfreaks.com/topic/53456-noob-error-i-think/#findComment-264192 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.