Jump to content

Compare variable with multiple words


kvnirvana

Recommended Posts

I need to compare the variable $q to multiple words (the, wood, host). Tried to do it like this but it doesn’t work. So if one of these words appears it will echo “his” otherwise it will echo “her”

 

<TR><br><p style='font-family:verdana;font-size:150%;'>how many {$_GET['ber']},  {$_GET['nn']}  are "; If ($q == 'the') or ($q == ’wood’) or ($q == ’host’) 
{ 
Echo " his $q?</p>"; 
        } 
Else{ 
Echo " her $q?</p>"; 
}  

Link to comment
https://forums.phpfreaks.com/topic/215352-compare-variable-with-multiple-words/
Share on other sites

That will work fine as long as you want the comparisons to be case sensitive. i.e. "wood" != "Wood".

 

If you need the comparison to be case insensitive you can do something similar to what you had before using strcasecmp()

 

 

if(strcasecmp($q, 'the')==0 || strcasecmp($q, 'wood')==0 || strcasecmp($q, 'host')==0)

 

 

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.