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 8 of 8
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    432
    Thanks
    6
    Thanked 3 Times in 3 Posts

    Can anyone explain the following PHP code?

    I have the following PHP code that someone did for me ages ago but can not remember who to ask them.

    I understand this much...
    Code:
    $username = !isset($username) ? $user['username'] : '' ;
    the above is the same as
    Code:
    if (!isset($username)) {
    $username = $user['username'];
    } else {
    $username = "";
    }


    But this code has me rather confused
    Code:
      $fullname = !isset($fullname) ? !isset($user) ? '' : $user['fullname'] : $fullname ;
      $mobile   = !isset($mobile) ? !isset($user) ? '' : $user['mobile'] : $mobile ;
      $email    = !isset($email) ? !isset($user) ? '' : $user['email'] : $email ;
      $email2    = !isset($email2) ? !isset($user) ? '' : $user['email'] : $email2 ;
    Can anyone explain what this actually does ?

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    First whoever wrote that should be thrown in my dungeon for excessive use of ternary expressions. Second they are using stacked ternaries meaning they are combining multiple ternaries in one statement. They evaluate from left to right. That is bad practice for the very reason that you are asking what it means.

    Another example of it part way down the page in the manual
    http://php.net/manual/en/language.op...comparison.php
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    New Coder
    Join Date
    Dec 2011
    Posts
    12
    Thanks
    6
    Thanked 1 Time in 1 Post
    The logic is equal to:

    if fullname is not set
    then
    if user is not set
    then
    set fullname = ""
    else
    set fullname = user
    else
    set fullname = fullname (in effect do nothing)

    The same holds for the other 2 statements.

  • Users who have thanked just.a.guy for this post:

    needsomehelp (01-05-2012)

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by Spookster View Post
    First whoever wrote that should be thrown in my dungeon for excessive use of ternary expressions. Second they are using stacked ternaries meaning they are combining multiple ternaries in one statement. They evaluate from left to right. That is bad practice for the very reason that you are asking what it means.
    Grump.

    Code:
    $fullname = (!isset($fullname) ? (!isset($user) ? '' : $user['fullname']) : $fullname);
    Slightly easier to understand using the more correct syntax above. The inner parantheses, (), enclose the expression which is evaluated if the initial outer !isset evaluates to true, with the final var being used if $fullname is set. It could also be written as:

    Code:
    $fullname = (isset($fullname) ? $fullname : (!isset($user) ? '' : $user['fullname']));

  • #5
    Regular Coder
    Join Date
    Oct 2009
    Posts
    432
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Ok I understand this now and will be changing the code to this method as the guy put loads of these in and just never got around to figuring it out until now when problems came up!

    Thanks for your help on this one.

    Quote Originally Posted by just.a.guy View Post
    The logic is equal to:

    if fullname is not set
    then
    if user is not set
    then
    set fullname = ""
    else
    set fullname = user
    else
    set fullname = fullname (in effect do nothing)

    The same holds for the other 2 statements.

  • #6
    Regular Coder
    Join Date
    Jun 2010
    Posts
    293
    Thanks
    63
    Thanked 8 Times in 8 Posts
    I use stacked ternaries sometimes (please don't throw me in the dungeon!), but I break them up between lines as per the following example:

    PHP Code:
    $result = ($cond1 $val1 :
               (
    $cond2 $val2 :
                (
    $cond3 $val3 :
                 
    $defaultVal))); 
    That makes the code a lot easier to read.

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,309
    Thanks
    58
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    The problem with stacked ternaries isn't so much that they're ugly or don't work, they can be very efficient however its the maintenance of the code afterwards.

    Supposing you need to change the code and include an extra php statement - I dunno, just something like "print $This;"

    Inside and if/else thats easy but inside a stacked ternary you're going to run into trouble.

    Ternaries do have their uses and are life savers for some things but the key to knowing when to use them is what you need to be skilled at understanding. One ternary, even a second stacked ternary is ok. Any more than that and you should switch to an if / else conditional instead.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #8
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by tangoforce View Post
    Ternaries do have their uses and are life savers for some things but the key to knowing when to use them is what you need to be skilled at understanding. One ternary, even a second stacked ternary is ok. Any more than that and you should switch to an if / else conditional instead.
    Sounds about right. Above two or so and they do become hellishly confusing to read, even when you're the one who wrote them.


  •  

    Posting Permissions

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