Forum Moderators: coopster

Message Too Old, No Replies

ternary issues

help with multiple ternary operations

         

Matthew1980

6:22 pm on Dec 28, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there people of webmaster world!

I know how to use ternary operator and do so wherever possible, but I have come across a particular comparison which has stopped me in my tracks:-

if ($main == "1")
{
$var = isset($_GET['op']) ? strip_tags($_GET['op']) : 'secure';
}
else
{
$var= isset($_GET['op']) ? strip_tags($_GET['op']) : 'standard';
}

Basically this if statement works fine at the moment, but I am trying to reduce my code wherever possible as I learn more about php, thus, I think as this particular example can be re-written to 1 multiple part ternary. I hope my theory is right anyway!

Thanks for any help given. Just to reiterate, as it is it works fine I just want to refine it.

Cheers,

MRb

FourDegreez

4:44 am on Dec 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm thinking, nested ternary operators?

(I like to keep them in parens for clarity)

$var = ($main == "1" ? (isset($_GET['op']) ? strip_tags($_GET['op']) : 'secure') : (isset($_GET['op']) ? strip_tags($_GET['op']) : 'standard'));

Seems like it should work.

Matthew1980

11:00 am on Dec 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks FourDegreez,

I shall copy and paste to see if this is what I need. Now as I have seen the way that you do this I think I understand the mechanics/logic behind it.

I'll let you know how I got on later.

Once again,

thanks

Matthew1980

11:08 am on Dec 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I couldn't resist it, I just altered it now, SWEET! Nice job FourDegreez. I didn't realize that you could use the ? as you have twice in the first part, but now as I read it, I now understand what is being said, simple when you logically analyse it.

Once again, thanks.

MRb