kvnirvana Posted October 7, 2010 Share Posted October 7, 2010 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>"; } Quote Link to comment https://forums.phpfreaks.com/topic/215352-compare-variable-with-multiple-words/ Share on other sites More sharing options...
jammesz Posted October 7, 2010 Share Posted October 7, 2010 Syntax error: If ($q == 'the') or ($q == ’wood’) or ($q == ’host’) Should be: if (($q == 'the') or ($q == ’wood’) or ($q == ’host’)) Quote Link to comment https://forums.phpfreaks.com/topic/215352-compare-variable-with-multiple-words/#findComment-1119865 Share on other sites More sharing options...
mikosiko Posted October 7, 2010 Share Posted October 7, 2010 I will do it in this way: $words = array('the', 'wood', 'host'); // Replace or add all the words that you wantif (in_array($q, $words) { echo " his $q?</p>";{ else { echo " her $q?</p>";} Quote Link to comment https://forums.phpfreaks.com/topic/215352-compare-variable-with-multiple-words/#findComment-1119874 Share on other sites More sharing options...
jammesz Posted October 7, 2010 Share Posted October 7, 2010 thats a better way of doing it but again you've missed out a ) character. if (in_array($q, $words) { should be: if (in_array($q, $words)) { Quote Link to comment https://forums.phpfreaks.com/topic/215352-compare-variable-with-multiple-words/#findComment-1119878 Share on other sites More sharing options...
mikosiko Posted October 7, 2010 Share Posted October 7, 2010 thanks... just typing too fast Quote Link to comment https://forums.phpfreaks.com/topic/215352-compare-variable-with-multiple-words/#findComment-1119882 Share on other sites More sharing options...
Psycho Posted October 7, 2010 Share Posted October 7, 2010 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) Quote Link to comment https://forums.phpfreaks.com/topic/215352-compare-variable-with-multiple-words/#findComment-1119890 Share on other sites More sharing options...
kvnirvana Posted October 7, 2010 Author Share Posted October 7, 2010 Thanks a lot for your help, very much appriciated :=) Quote Link to comment https://forums.phpfreaks.com/topic/215352-compare-variable-with-multiple-words/#findComment-1119927 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.