Jump to content


Photo

Checking a variable against a number of values


  • Please log in to reply
4 replies to this topic

#1 AbydosGater

AbydosGater
  • Members
  • PipPipPip
  • Advanced Member
  • 435 posts
  • LocationAlways at computer!

Posted 12 March 2006 - 03:02 PM

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

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

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
www.abydosgaters.com

Current Project: Blog Application.. Undecided name.. Status: Coming along great.

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 12 March 2006 - 03:12 PM

Take a look at the [a href=\"http://ca.php.net/manual/en/function.in-array.php\" target=\"_blank\"]in_array function[/a] - that should do it for you.
Legend has it that reading the manual never killed anyone.
My site

#3 AbydosGater

AbydosGater
  • Members
  • PipPipPip
  • Advanced Member
  • 435 posts
  • LocationAlways at computer!

Posted 12 March 2006 - 04:11 PM

I find this very confusing,
Would there be any other way?

www.abydosgaters.com

Current Project: Blog Application.. Undecided name.. Status: Coming along great.

#4 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 12 March 2006 - 04:14 PM

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) View Post[/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]


#5 AbydosGater

AbydosGater
  • Members
  • PipPipPip
  • Advanced Member
  • 435 posts
  • LocationAlways at computer!

Posted 12 March 2006 - 04:38 PM

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,

if ($myvar != 'value1' || $myvar != '2' || and so on?
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



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



???
www.abydosgaters.com

Current Project: Blog Application.. Undecided name.. Status: Coming along great.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users