Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.