Jump to content

noob error (i think)


map200uk

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/53456-noob-error-i-think/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/53456-noob-error-i-think/#findComment-264185
Share on other sites

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=]

 

 

Link to comment
https://forums.phpfreaks.com/topic/53456-noob-error-i-think/#findComment-264188
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/53456-noob-error-i-think/#findComment-264191
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.