Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Regular Coder
    Join Date
    Apr 2004
    Location
    Los Angeles
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Abbreviated "If" Statement

    Is there a short form of:
    if($var=="this"||$var=="that")

    Such as:
    if($var=="this"||"that")

  • #2
    Regular Coder
    Join Date
    Sep 2002
    Location
    British Columbia
    Posts
    235
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No, nothing shorter that that first one. That's tthe way boolean loggic works.

    This is what I do if I have to test for more than one value for a single variable. You can add as many elements to the array as you please.

    if (in_array($var, array('this', 'that')))

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Sorry, none that I am aware of. Of course, there are easier ways if you are using mulitple criteria, and longer ways as well. But what you have is pretty much the easiest. I'd recommend single quoting though, and not using pipebars, because if you have numbers and forget the quotes, you'll be attempting to validate a bitwise value instead.

    PHP Code:
    if ($var == 'this' OR $var == 'that')
    {
         
    //Do whatever.

    Oops, a little late, but meh.

  • #4
    Regular Coder
    Join Date
    Apr 2004
    Location
    Los Angeles
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Fou and Dylan.

  • #5
    New to the CF scene
    Join Date
    Oct 2004
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    switch($var){
    case "this";
    case "that":{/*
    your code here...
    */}
    }

    __or__

    if(strpos("thisthat",$var)){...}

    (Not shure if this works. It would match $var="isth", or $var="s", for instance.)

    and as above

    in_array($var,['this','that'])?/*your code here*/:/*else*/;
    Last edited by ghaspias; 10-07-2004 at 01:05 AM.

  • #6
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by Fou-Lu
    I'd recommend single quoting though, and not using pipebars, because if you have numbers and forget the quotes, you'll be attempting to validate a bitwise value instead.

    How do you figure that? A bitwise OR uses one |. In this case they are going to be using ||. I don't see how quoting can cause that to do a bitwise operation.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Oops, I suppose so eh? Maybe I'm just on crack!

    Still, I'd use the or, the pipebars are kinda depreciated.

  • #8
    Regular Coder
    Join Date
    Apr 2004
    Location
    Los Angeles
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts
    FYI, Pipebars aren't depreciated. In fact they have a higher precedance than "OR":

    http://us4.php.net/manual/en/languag...ors.precedence

  • #9
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Quote Originally Posted by Fou-Lu
    Oops, I suppose so eh? Maybe I'm just on crack!

    Still, I'd use the or, the pipebars are kinda depreciated.
    Put your hands in the air and step away from the crack pipe.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #10
    Regular Coder
    Join Date
    May 2002
    Location
    Virginia, USA
    Posts
    621
    Thanks
    0
    Thanked 6 Times in 6 Posts
    perhaps:

    PHP Code:
    (($this == "that" ) || ($this == "this")) ? return true : return false

  • #11
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ternaries are fun. =)

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by Hawkmoon
    FYI, Pipebars aren't depreciated. In fact they have a higher precedance than "OR":

    http://us4.php.net/manual/en/languag...ors.precedence
    Now I'm confused. I've always used OR / AND with any non integer variable because I thought they were a higher precedence. Hmm, question then, is there any practical use of pipebars taking precedence over the OR when it comes to the non-integer variables? Probably not, but I guess I should check to see if anyone has had problems in the past.

  • #13
    Regular Coder
    Join Date
    Apr 2004
    Location
    Los Angeles
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah...don't know that there is any real USEFUL application for the precedence...I guess potentially for the following:
    PHP Code:
    if($var=="this" || $var=="that" OR $var2=="this") {
      
    //If you wanted it to see if $var matched those values before you checked $var2


  • #14
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    That would make no sense though. You are checking to see IF $var is equal to this or that, or if $var2 is equal to that. Now, if you wanted to check something like $var1 is equal to this AND $var2 is equal to this or that, you would use something like this:

    PHP Code:
    if ($var == "this" && ($var2 == "this" || $var2 == "that") { /*content */ 

  • #15
    Regular Coder
    Join Date
    Apr 2004
    Location
    Los Angeles
    Posts
    133
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Kurashu -
    The above example was to show the precedence of pipebars over "OR" and was not addressing my original question. See Spookster's and my comments to Fou-Lu about pipebars.

    -Hawkmoon


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •