Ternary operator question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Ternary operator question

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

right, but

Code: Select all

echo $row['year']=='0000' ? '' : $row['year'];
works ;)
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ;)
User avatar
cactus
Forum Regular
Posts: 343
Joined: Tue Jun 10, 2003 4:16 am
Location: UK

Post 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,
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post 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
Post Reply