stephenk Posted July 27, 2007 Share Posted July 27, 2007 Hi, I know there is a sortcut to do the following, but I cannot find it anywhere. <?php if($a = "1" or $a = "2" or $a="3") {} else {} ?> likewise ... <?php if(eregi("abc", $1) or eregi("bcd",$1) or eregi("cde",$1)) {} else {} ?> I think it involves square brackets and the | symbol, but I can't get the combination right! Many thanks, Stephen Quote Link to comment Share on other sites More sharing options...
stephenk Posted July 27, 2007 Author Share Posted July 27, 2007 I've worked out the eregi one, it can be done using: <?php if(eregi("(abc|bcd|cde)",$1) {} else {} ?> Anyone got any ideas for the if statement? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted July 27, 2007 Share Posted July 27, 2007 I dont think there is any way to do the same thing with your original if statement, unless you were to assign all of the values to an array, and use the in_array function. However, i would imagine that this would be a slower method, so unless you've got huge numbers of possible values to test, you might as well just type them out. Edit: Of couse, if you were just testing with a series of numbers you could just test to see if your variable was greater than or equal to the lowest number, and less than or equal to the highest: <?php if($a >= 1 && $a <= 3){ }else{ } ?> Quote Link to comment Share on other sites More sharing options...
per1os Posted July 27, 2007 Share Posted July 27, 2007 <?php if($a = "1" or $a = "2" or $a="3") {} else {} ?> That will always return true. Use == for comparison not a single = Like wise you could do this: <?php if(ereg("1|2|3", $a)) {} else {} ?> or <?php if(ereg("([1-3])", $a)) {} else {} ?> Should work too. Quote Link to comment Share on other sites More sharing options...
stephenk Posted July 27, 2007 Author Share Posted July 27, 2007 <?php if($a = "1" or $a = "2" or $a="3") {} else {} ?> That will always return true. Use == for comparison not a single = Yeah, well spotted, I usually do that, but the code I quickly wrote out was just for simplicity, not code from my script Like wise you could do this: <?php if(ereg("1|2|3", $a)) {} else {} ?> or <?php if(ereg("([1-3])", $a)) {} else {} ?> Should work too. Thanks for that, good idea, however because of the nature of ereg, it obviously won't always be suitable! Likewise with GingerRobot's solution. I just assumed that there would be some sort of way to do it when not using numbers (which I just used as an example). Cheers, Stephen Quote Link to comment 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.