You are here: Re: shortif (tertiary) fails with echo but not print ? « PHP Programming Language « IT news, forums, messages
Re: shortif (tertiary) fails with echo but not print ?

Posted by Rami Elomaa on 05/02/07 10:27

"Tyno Gendo" <you@localhost> wrote in message
news:fsqdnSgBg7PY0KXbnZ2dnUVZ8qjinZ2d@bt.com...
> 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

All functions return something. If no return stetment is used, it still
returns NULL. The difference in echo is that it's a language construct like
while, if, else and foreach, they are not functions. :)

--
Rami.Elomaa@gmail.com

"Good tea. Nice house." -- Worf

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация