Results 1 to 7 of 7

Thread: small if statement

  1. #1
    Join Date
    Aug 2006
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default small if statement

    Hey, how did that small if statement work in php?

    something like.. (variable == something : (this is true) ?(this is false))?

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    What the hell are you talking 'bout man?
    EDIT: oh, it works something like this:
    Code:
    if(condition)
    Jeremy | jfein.net

  3. #3
    Join Date
    Aug 2006
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Nile View Post
    What the hell are you talking 'bout man?
    EDIT: oh, it works something like this:
    Code:
    if(condition)
    thats a regular if statement, I talking about the shorter one.

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Theres no shorter if statement, form what I know.
    Jeremy | jfein.net

  5. #5
    Join Date
    Aug 2006
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Nile View Post
    Theres no shorter if statement, form what I know.
    yes there is, i just tested different combinations and found out that it works like this:

    Code:
    $nr = 2;
    
    echo ($nr == 2 ? 'yes' : 'no');
    it will print yes, but if i write $nr = 3 it will print no

  6. #6
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    I'm impressed. There really is a shorter way of doing it, however you could be a little clearer in your question. It was a little bit difficult to understand what you were asking about at first.

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I like this compact statement, but it can also be incredibly confusing to read, so use with discretion.

    Here's how it works... quite simply, really, but odd to wrap your brain around at first:
    condition ? if true do this : else this;
    Or: condition [is it true?] do this [else:] this;

    So, here's an example:
    ($a==1) ? echo 1 : echo 'not 1';

    However, this can also be embedded and you can get very compact ways of using an if within something else:
    echo 'We have '.$a.' item'.($a>1?'s':'').' for sale.';

    You can even layer it, where the else portion is a second such statement, allowing you a result for <1, then a result for >5 if desired, making a long statement.

    But remember as I said this can become very hard to read.
    Last edited by djr33; 02-03-2008 at 11:14 PM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

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