1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Example for alternate if

Discussion in 'PHP' started by Tony Brar, Nov 18, 2012.

  1. #1
    Hi guys,

    I have come across this more than once
    (condition ? action_if_true: action_if_false;)
    I want to be able to understand it.
    Can someone give me a working example?

    Thanks,
    -Tony
     
    Tony Brar, Nov 18, 2012 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    normaly we can do this

    
    if (isset($_GET['parameter']))
    {
         $param = $_GET['parameter'];
    }
    else
    {
        $param = '';
    }
    
    Code (markup):
    but the same can be writen on one line as this

    
    $param = isset($_GET['parameter']) ? $_GET['parameter'] : '';
    
    Code (markup):
    you can tell it like this
    $param IS is variable set $_GET['parameter'] YES WRITE $_GET['parameter'] NO WRITE ''
     
    EricBruggema, Nov 18, 2012 IP
  3. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #3
    I have another question.
    Can this apply to doing something, instead of just setting a variable?
    I mean, could you shorten this?
    
    if($mine==false){echo 'User Page';}else{echo 'My User Page';}
    
    PHP:
    Thanks,
    -Tony
     
    Tony Brar, Nov 18, 2012 IP
  4. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #4
    echo ($mine == false) ? "user page" : "my user page";
     
    EricBruggema, Nov 18, 2012 IP
  5. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #5
    Okay...
    This?
    (assume foo and bar are functions, each with one parameter)
    
    if($mine==false)
    {
    foo('testvalue');
    }
    else
    {
    bar(28);
    }
    
    PHP:
    Thanks,
    -Tony
     
    Tony Brar, Nov 19, 2012 IP
  6. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #6
    this should be like this but when you want to return a variable to a string use the methods from above.
     
    EricBruggema, Nov 20, 2012 IP
  7. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #7
    So it cannot be used to select an action, only to select a variable?

    -Tony
     
    Tony Brar, Nov 20, 2012 IP
  8. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #8
    My opinion:

    I wouldn't use this method. Code will get more complicated. I like to keep my code as simple as possible and in a format that will work on every server etc.
     
    stephan2307, Nov 20, 2012 IP
  9. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #9
    I agree stephan2307, but when you also do alot of things in templates, this will come in handy! :)
     
    EricBruggema, Nov 20, 2012 IP
  10. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #10
    Is there a way to spread this over multiple lines? (if I had long strings to echo)
    
    echo ($mine == false) ? "user page" : "my user page";
    
    PHP:
    Just so I could see my code better, in a minimized window.

    Thanks,
    -Tony
     
    Tony Brar, Nov 20, 2012 IP
  11. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #11
    
    echo ($mine == false) ? "user\n page" : "my\n user\n page";
    
    PHP:
     
    stephan2307, Nov 20, 2012 IP
  12. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #12
    or use <br /> instead of \n
     
    EricBruggema, Nov 20, 2012 IP
  13. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #13
    I meant spreading the source code out.
    For example, could:
    
    $foo=true;
    echo ($foo==true) ? "this is such a crazily long string its so long its insane i want to spread it out in my code" : "shortstring";
    
    PHP:
    be spread out to:
    
    $foo=true;
    echo ($foo==true) ? "this is such a crazily long string its so long its 
     insane i want to spread it out in my code" : "shortstring";
    
    PHP:
    without an error?
    Because $foo=true, I would want the above snippet to result in:
    this is such a crazily long string its so long its insane i want to spread it out in my code
    Would that work?

    Thanks,
    -Tony
     
    Tony Brar, Nov 21, 2012 IP
  14. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #14
    put () around it

    so echo (($foo == true) ? "" : "");

    :)
     
    EricBruggema, Nov 21, 2012 IP
  15. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #15
    So, like this?

    
    echo (($foo==true) ? "this is such a crazily long string its so long its 
     insane i want to spread it out in my code" : "shortstring");
    
    PHP:
    That would work fine?

    -Tony
     
    Tony Brar, Nov 21, 2012 IP
  16. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #16
    I assume yes! but have you tried it?
     
    EricBruggema, Nov 21, 2012 IP
  17. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #17
    Just tried it, works fine!

    Thanks,
    -Tony
     
    Tony Brar, Nov 22, 2012 IP
  18. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #18
    If you need more than one line, I'd use HEREDOC to set a variable with that text, then echo the variable. It's neater.

    
    //from the manual
    [COLOR=#000000][COLOR=#0000BB]$str [/COLOR][COLOR=#007700]= <<<EOD
    [/COLOR][COLOR=#DD0000]Example of string
    spanning multiple lines
    using heredoc syntax.
    [/COLOR][COLOR=#007700]EOD;
    
    [/COLOR][/COLOR]
    [FONT=monospace][COLOR=#b1b100]echo[/COLOR] [COLOR=#009900]([/COLOR][COLOR=#000088]$mine[/COLOR] [COLOR=#339933]==[/COLOR] [COLOR=#009900][B]false[/B][/COLOR][COLOR=#009900])[/COLOR] ? [COLOR=#0000ff]"user page"[/COLOR] [COLOR=#339933]:[/COLOR] $str[COLOR=#339933];[/COLOR]
    [/FONT]
    Code (markup):

     
    Rukbat, Nov 22, 2012 IP
  19. Tony Brar

    Tony Brar Active Member

    Messages:
    220
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    75
    #19
    Why HEREDOC?
    Why not just
    
    $str = 'Example of string
    spanning multiple lines
    using single quotes.';
    
    PHP:
    -Tony
     
    Tony Brar, Nov 22, 2012 IP