adibranch Posted March 9, 2011 Share Posted March 9, 2011 Hi all, i've got a string ($currentcatid) which contains one number which changes dependant on which page the visitor is on.. ie it may be perhaps "23" or "17" . I need to do a check against this string to see if it contains any of the following values .. 31,32, 1, 24 . I cant remember how i did it before but i think it was with pipes.. something along the lines of if ($currentcatid =="31"||"32"||"1"||"24") { action } can anyone help? Thanks Quote Link to comment Share on other sites More sharing options...
flolam Posted March 9, 2011 Share Posted March 9, 2011 what is the problem with the code you have? if you want to check whether the string contains this number, you will have to use a function like strstr (http://php.net/manual/en/function.strstr.php), because currently you are checking whether the variable has exactly this value Quote Link to comment Share on other sites More sharing options...
adibranch Posted March 9, 2011 Author Share Posted March 9, 2011 the problem with the code i posted was that it only checks for the first value eg in this case "31" . What i'm basically asking it to do .. Is the value in $currentcat equal to any of these values : 31, 32,1, or 24 ? If so, please do {action} Quote Link to comment Share on other sites More sharing options...
flolam Posted March 9, 2011 Share Posted March 9, 2011 this does work on my machine: $a = "31"; if ($a == "29" || "30" || "31" || "32") { echo "true"; } //output: true --Edit-- I just noticed that this will always return true, no matter what value $a has Quote Link to comment Share on other sites More sharing options...
flolam Posted March 9, 2011 Share Posted March 9, 2011 of course, this will always work: $a = "31"; if ($a == "29" || $a == "30" || $a == "31" || $a == "32") { echo "true"; } --edit-- or: $a = "31"; $b = array("31", "32"); if (in_array($a, $b)) { echo "true"; } Quote Link to comment Share on other sites More sharing options...
adibranch Posted March 9, 2011 Author Share Posted March 9, 2011 thanks i had already used the top code and sorted it after reading somewhere on here, but the bottom one tidied it up nicely. Quote Link to comment 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.