CodeIgniter Forums
Ternary vs. Long method condition - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Ternary vs. Long method condition (/showthread.php?tid=22018)



Ternary vs. Long method condition - El Forum - 08-27-2009

[eluser]aroman[/eluser]
Heres the scenario:
Code 1:
Code:
$tmptitle = $this->input->post( 'title' );
if ( !empty($tmptitle) )
{
   $title = $tmptitle ;
}
else
{
  if ( !empty($bannerads->title) )
  {
     $title = $bannerads->title ;
  }
  else
  {
     $title = '' ;
  }
  
  echo $title ;
}


Code 2:
Code:
$title = !empty($tmptitle) ? $tmptitle : !empty($bannerads->title) ?  ($bannerads->title) : '' ;
echo $title ;

Executing the code#1, it works howver when executing the Code#2 i received an error. It says "Severity: Notice
Message: Trying to get property of non-object ..."
Is this a bug in ternary operation or in CI ?

Thanks
-armano


Ternary vs. Long method condition - El Forum - 08-27-2009

[eluser]Dam1an[/eluser]
a) That snippet of code there isn't really anything to do with CI, so not a CI problem
b) Nested ternaries are just messy Wink
c) What happens if you but brackets around the nested part?


Ternary vs. Long method condition - El Forum - 08-27-2009

[eluser]aroman[/eluser]
@dam1a , yeah nested ternaries are messy if it is very long..but in these case, i would love to use the ternary.
i havnt put a bracket[] yet, is it possible?? hmm, where should i put it ? can you show me how?

thanks


Ternary vs. Long method condition - El Forum - 08-27-2009

[eluser]Dam1an[/eluser]
A little bit of laying around with it, and the only way I could get it to fail was with
Code:
$title = !empty($tmptitle) ? $tmptitle : !empty($bannerads->title) ?  ($bannerads->title) : '';
var_dump($title);

But adding brackets to the nested part like so
Code:
$title = !empty($tmptitle) ? $tmptitle : (!empty($bannerads->title) ?  ($bannerads->title) : '');
var_dump($title);

fixes it Smile (I tried all paths and it works fine for me)


Ternary vs. Long method condition - El Forum - 08-27-2009

[eluser]aroman[/eluser]
Hi dam1an, it works now..thank you so much..im wondering why we need to put a bracket for the second else part, however in the first part dont have it..hmmm.


Ternary vs. Long method condition - El Forum - 08-27-2009

[eluser]Dam1an[/eluser]
In the first part you don;t need to, as it's just a single, atomic value, instead of an expression
I think you eed brackets for the second part, as it will probably just assume !empty($bannerads->title) is the else part of the statement, instead of actually doing the whole of the else part, but the bracket forces it to do so


Ternary vs. Long method condition - El Forum - 08-27-2009

[eluser]Bogdan Tanase[/eluser]
aroman, are you sure you'll understand a month from now what you did there ? Smile

ternaries are very useful for simple expressions; using it like that is just plain bad ... IMHO