AbydosGater Posted March 12, 2006 Share Posted March 12, 2006 HiI 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 youAndrew Butler Quote Link to comment https://forums.phpfreaks.com/topic/4752-checking-a-variable-against-a-number-of-values/ Share on other sites More sharing options...
AndyB Posted March 12, 2006 Share Posted March 12, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/4752-checking-a-variable-against-a-number-of-values/#findComment-16677 Share on other sites More sharing options...
AbydosGater Posted March 12, 2006 Author Share Posted March 12, 2006 I find this very confusing,Would there be any other way? Quote Link to comment https://forums.phpfreaks.com/topic/4752-checking-a-variable-against-a-number-of-values/#findComment-16691 Share on other sites More sharing options...
hitman6003 Posted March 12, 2006 Share Posted March 12, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/4752-checking-a-variable-against-a-number-of-values/#findComment-16694 Share on other sites More sharing options...
AbydosGater Posted March 12, 2006 Author Share Posted March 12, 2006 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]??? Quote Link to comment https://forums.phpfreaks.com/topic/4752-checking-a-variable-against-a-number-of-values/#findComment-16703 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.