|
Posted by Jerry Stuckle on 05/02/07 11:13
Tyno Gendo wrote:
> Jerry Stuckle wrote:
>> Tyno Gendo wrote:
>>> I have just written a line of code like this (yes, many people think
>>> this stinks bad):
>>>
>>> <?php true == isset($page_seq) ? echo "$page_seq" : false; ?>
>>>
>>> However it breaks with 'Unexpected T_ECHO'
>>>
>>> But
>>>
>>> <?php true == isset($page_seq) ? print "$page_seq" : false; ?>
>>>
>>> Works as expected.
>>>
>>> Anyone know why? Maybe its blindly obvious to someone, that time of
>>> the day when the brain SLLLOOOOOOOWWS down.
>>
>> It fails because the ternary operator requires the true and false
>> clauses to return values. echo does not return a value. print() does.
>>
>> It's basically the *very subtle* difference between the two.
>>
>
> Thanks Jerry, I did notice that in the manual.
>
> Are there any other functions that return void? I tried the following
> and they didn't product errors:
>
> function test() {
> // no return value, confirmed with echo test();
> }
>
> 1 == 1 ? test() : false;
>
> This worked fine but returns no return value.
>
> And...
>
> 1 == 1 ? exit("yes, its was true") : false;
>
> My function doesn't return anything and exit has a void return although
> I can see exit might work as its a script terminator so perhaps the
> tertiary doesn't care as the script is ending.
That's because echo is a language construct, not a function. The error
you saw is a parse-time error. Functions are assumed to return a value
by the parser.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
Navigation:
[Reply to this message]
|