triyng to understand this code

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
iffo
Forum Commoner
Posts: 37
Joined: Thu Oct 05, 2006 11:56 am

triyng to understand this code

Post by iffo »

Hi,

I have never seen this syntax what is going on here?


echo ($this->pixel=="iframe")?$this->affil_pixel():"";

Thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It's called a ternary or trinary operator.

Basically, it's a compact version of and if..else construct.

expression1 ? expression2 : expression3 is analogous to if( expression1 ) expression2; else expression3;
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

something that should have been obvious, but I just realized recently is that it's called the ternary operator because it's the only operator that works on three expressions at once... :oops:
iffo
Forum Commoner
Posts: 37
Joined: Thu Oct 05, 2006 11:56 am

Post by iffo »

thanks I understand now, but why echo? what role echo is playing here ?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

echo() prints to the screen (similar to print()).
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

So, to extend that to feyd's example...
echo expression1 ? expression2 : expression3 is analogous to if( expression1 ) echo expression2; else echo expression3;
iffo
Forum Commoner
Posts: 37
Joined: Thu Oct 05, 2006 11:56 am

Post by iffo »

perfect ....... thanks a lot
Post Reply