...

View Full Version : Can anyone explain the following PHP code?



needsomehelp
01-05-2012, 02:31 AM
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...


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

the above is the same as


if (!isset($username)) {
$username = $user['username'];
} else {
$username = "";
}




But this code has me rather confused


$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 ?

Spookster
01-05-2012, 02:44 AM
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.operators.comparison.php

just.a.guy
01-05-2012, 05:41 AM
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.

MattF
01-05-2012, 06:02 AM
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. :D



$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:



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

needsomehelp
01-05-2012, 12:34 PM
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.


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.

XmisterIS
01-05-2012, 02:23 PM
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:



$result = ($cond1 ? $val1 :
($cond2 ? $val2 :
($cond3 ? $val3 :
$defaultVal)));


That makes the code a lot easier to read.

tangoforce
01-05-2012, 02:26 PM
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.

MattF
01-05-2012, 06:40 PM
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. :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum