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 Link to comment https://forums.phpfreaks.com/topic/230081-check-string-for-multiple-values/ 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 Link to comment https://forums.phpfreaks.com/topic/230081-check-string-for-multiple-values/#findComment-1184939 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} Link to comment https://forums.phpfreaks.com/topic/230081-check-string-for-multiple-values/#findComment-1184947 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 Link to comment https://forums.phpfreaks.com/topic/230081-check-string-for-multiple-values/#findComment-1184950 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"; } Link to comment https://forums.phpfreaks.com/topic/230081-check-string-for-multiple-values/#findComment-1184952 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. Link to comment https://forums.phpfreaks.com/topic/230081-check-string-for-multiple-values/#findComment-1184973 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.