Jump to content

Shortcuts - if() and eregi()


stephenk

Recommended Posts

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

Link to comment
Share on other sites

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{
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.