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.
Results 1 to 4 of 4
  1. #1
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts

    Can somebody help me break down this 1 line of code ??

    I'm reviewing someone elses code, and came across this line:
    PHP Code:
    <?php echo ($view=='folded')? 'folded_form_step':'form_step';?>
    There are several things I'd like to learn here.

    For starters, what is ($view=='folded')? all about? This is the first time I've seen anything like that. Is that shorthand for an IF statement or something ?
    Next, what's with the : (colon)?

    Can someone walk me through what's being done here ??

    FYI: this is in a class which builds forms, and $folded_form_step & $form_step are vars which get passed into the class, but the code in question does not use the dollar signs. Are they related?

    ~ Mo
    Last edited by mOrloff; 02-17-2010 at 09:21 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Ternary operator.
    ($view == 'folded') <- Indicates a condition. Techincally the brackets are not required
    ? expression <- What to do if its true
    : expression <- What do to if its false

    In 5.3 (I think its 5.3 it was introduced) you can actually do a shorthanded ternary for just the else:
    ($d == 'data') ?: 'no match';
    PHP Code:
    echo ($view=='folded')? 'folded_form_step':'form_step';
    // is the same as
    if ($view == 'folded')
    {
        echo 
    'folded_form_step';
    }
    else
    {
        echo 
    'form_step';

    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,237
    Thanks
    12
    Thanked 340 Times in 336 Posts
    Quote Originally Posted by Fou-Lu View Post
    In 5.3 (I think its 5.3 it was introduced) you can actually do a shorthanded ternary for just the else:
    ($d == 'data') ?: 'no match';
    the true case returns the evaluation of the condition. i.e. this code would return 1 if $d = "data".
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #4
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Ahah!

    Thanks-a-bunch.


  •  

    Posting Permissions

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