Jump to content

Checking a variable against a number of values


AbydosGater

Recommended Posts

Hi
I was editing my site the oder day and i came up with an idea,
but i need to be able to check a variable against a number of values...
as in

[code]
if ($myvar != ***here I want to have a number of things that it might not be***)
    {
$myvar = "defaultvars";
    }
[/code]

so if my variable is not equal to a number of values, it is changed to the default one, like "index"

How would i do this for the bit
($myvar != how would i list my values here?

Thank you
Andrew Butler
Link to comment
Share on other sites

There are lots of other ways.

[!--quoteo(post=354205:date=Mar 12 2006, 11:11 AM:name=AbydosGater)--][div class=\'quotetop\']QUOTE(AbydosGater @ Mar 12 2006, 11:11 AM) [snapback]354205[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I find this very confusing,
[/quote]

in_array is probably the easiest, especially if you have more than 3 or 4 values that you are wanting to compare.

Alternatives include using a very long if statement:

if ($value == '1' || $value == '2' || $value == '3' etc...) {

or using a switch statement:

[a href=\"http://www.php.net/switch\" target=\"_blank\"]http://www.php.net/switch[/a]
Link to comment
Share on other sites

The switch woulnt work because i want to check the variable against about 5 values,
and if it does not match any of the values, i want to assign it the value "index",
So if someone for example types in "pizza", i want it to change the variable to index, so there are no errors!

But i think this will work...
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
if ($value == '1' || $value == '2' || $value == '3' etc...) {
[/quote]

so what would i do,

[code]if ($myvar != 'value1' || $myvar != '2' || and so on?[/code]
Would that be right?

Thats what i was going to do in the first place, But i didnt know what to put in between all the values,
i was going to use "&&" instead of "||",
But will the above work?
so in total it would be like



[code]
if ($myvar != 'value1' || $myvar != 'value2' || $myvar != 'value3' || $myvar != 'value4'
    {
    $myvar = "index"
     }
[/code]



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