Page 1 of 1

Ternary operator question

Posted: Fri Jun 13, 2003 3:01 pm
by BDKR
You would think this would work, but it doesn't.

Code: Select all

$row['year']=='0000' ? echo '' : echo $row['year'] ;
But this does.

Code: Select all

$row['year']=='0000' ? print '' : print $row['year'] ;
Go figure eh? Does it have something to do with echo not really being a function? If I'm not mistaken, echo is a language construct correct?

Cheers,
BDKR

Posted: Fri Jun 13, 2003 3:40 pm
by volka
right, but

Code: Select all

echo $row['year']=='0000' ? '' : $row['year'];
works ;)

Posted: Fri Jun 13, 2003 4:11 pm
by BDKR
volka wrote:right, but

Code: Select all

echo $row['year']=='0000' ? '' : $row['year'];
works ;)
Volka, I have to say that that's the uglies hunk of code I've ever seen. I thought ternary operators were bad. Are you sure that's not Perl? Seriously, what is it?

Cheers,
BDKR

Posted: Fri Jun 13, 2003 4:14 pm
by BDKR
Oooohh, wait a sec! Is this thing testing the output of echo, then making a decision based on that? If so, that's a cool trick! I'll have to keep that one in mind. Now I see why your moniker is extreme Guru.

Thanx,
BDKR

Posted: Fri Jun 13, 2003 4:18 pm
by cactus
Shhh, there may be Perl developers around ;) To be honest I do that when I'm lazy or if I'm prototyping, it's a quick way to do an if/else, or a lazy one, your choice.

Not exactly plain to see what going on, but once you have it sussed it kinda grows on you.

Regards,

PS: I've only ever used it in Perl and PHP.

Posted: Fri Jun 13, 2003 4:23 pm
by volka
BDKR wrote:Volka, I have to say that that's the uglies hunk of code I've ever seen.
You see, beauty is in the eye of the beholder ;)

Posted: Fri Jun 13, 2003 4:26 pm
by cactus
In fact. I would go so far as to defend it if PHP decided to remove it from the feature set. Won't go on a demonstration or anything, but probably TYPE IN CAPS FOR A WHILE ;)

Regards,

Posted: Fri Jun 13, 2003 4:36 pm
by BDKR
Well, I hope they don't remove it personally. I've allways liked ternary operators, but the issue earlier threw me a curve.

Also, I think Perl is cool, even if it can be tough to read at times.

Cheers,
BDKR