Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is wrong with this syntax while using ( condition ? true : false )??? 1

Status
Not open for further replies.

southbeach

Programmer
Jan 22, 2008
879
US
I have code such as
Code:
$blank='';
$string = '
<td>
<input name="itemName" id="itemName" type="text" size="12" maxlength="20" 
value="'.isset($line['name'])?$line['name']:$blank.'" />
</td>';

It is not working! Why?



--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Well, I went right back to my code and changed it to
Code:
$blank='';
$string = '
<td>
<input name="itemName" id="itemName" type="text" size="12" maxlength="20" 
value="'.(isset($line['name'])?$line['name']:$blank).'" />
</td>';

I wrapped the condition within parenthesis and it works now ... I do not see why this makes such a difference ...


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Hi

SouthBeach said:
I wrapped the condition within parenthesis and it works now ... I do not see why this makes such a difference ...
It is because the Operator Precedence : concatenation has higher priority than the ternary operator.

So without parenthesis the concatenations get evaluated first :
PHP:
$string=[highlight #fcc]'before'.'condition'[/highlight]?[highlight #cfc]'truevalue'[/highlight]:[highlight #ccf]'falsevalue'.'after'[/highlight];

[gray]// same as[/gray]

$string=[highlight #fcc]'beforecondition'[/highlight]?[highlight #cfc]'truevalue'[/highlight]:[highlight #ccf]'falsevalueafter'[/highlight];


Feherke.
feherke.github.io
 
Goes to show that you are taught the very basic things for a very basic reason!

Thanks,


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top