Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-17-2010, 09:18 PM   PM User | #1
mOrloff
Regular Coder

 
mOrloff's Avatar
 
Join Date: Nov 2008
Location: The Great Pacific NW, USA
Posts: 421
Thanks: 8
Thanked 6 Times in 6 Posts
mOrloff is an unknown quantity at this point
Can somebody help me break down this 1 line of code ??

I'm reviewing someone elses code, and came across this line:
PHP Code:
<?php echo ($view=='folded')? 'folded_form_step':'form_step';?>
There are several things I'd like to learn here.

For starters, what is ($view=='folded')? all about? This is the first time I've seen anything like that. Is that shorthand for an IF statement or something ?
Next, what's with the : (colon)?

Can someone walk me through what's being done here ??

FYI: this is in a class which builds forms, and $folded_form_step & $form_step are vars which get passed into the class, but the code in question does not use the dollar signs. Are they related?

~ Mo

Last edited by mOrloff; 02-17-2010 at 09:21 PM..
mOrloff is offline   Reply With Quote
Old 02-17-2010, 09:23 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Ternary operator.
($view == 'folded') <- Indicates a condition. Techincally the brackets are not required
? expression <- What to do if its true
: expression <- What do to if its false

In 5.3 (I think its 5.3 it was introduced) you can actually do a shorthanded ternary for just the else:
($d == 'data') ?: 'no match';
PHP Code:
echo ($view=='folded')? 'folded_form_step':'form_step';
// is the same as
if ($view == 'folded')
{
    echo 
'folded_form_step';
}
else
{
    echo 
'form_step';

__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 02-17-2010, 10:14 PM   PM User | #3
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,855
Thanks: 9
Thanked 288 Times in 284 Posts
Dormilich is on a distinguished road
Quote:
Originally Posted by Fou-Lu View Post
In 5.3 (I think its 5.3 it was introduced) you can actually do a shorthanded ternary for just the else:
($d == 'data') ?: 'no match';
the true case returns the evaluation of the condition. i.e. this code would return 1 if $d = "data".
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 02-17-2010, 11:10 PM   PM User | #4
mOrloff
Regular Coder

 
mOrloff's Avatar
 
Join Date: Nov 2008
Location: The Great Pacific NW, USA
Posts: 421
Thanks: 8
Thanked 6 Times in 6 Posts
mOrloff is an unknown quantity at this point
Ahah!

Thanks-a-bunch.
mOrloff is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:14 AM.


Advertisement
Log in to turn off these ads.