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



???

Archived

This topic is now archived and is closed to further replies.

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