Jump to content

check string for multiple values


adibranch

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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