By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,216 Members | 1,032 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,216 IT Pros & Developers. It's quick & easy.

shortif (tertiary) fails with echo but not print ?

P: n/a
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.
May 1 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Tyno Gendo kirjoitti:
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's mentioned vaguely in the manual:

"echo() is not actually a function (it is a language construct), so you
are not required to use parentheses with it. echo() (unlike some other
language constructs) does not behave like a function, so it cannot
always be used in the context of a function."

http://fi.php.net/manual/en/function.echo.php
Also read this:
http://www.faqts.com/knowledge_base/...l/aid/1/fid/40

--
Ra*********@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
May 1 '07 #2

P: n/a
Tyno Gendo kirjoitti:
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; ?>
You could make this shorter ;)

<?php isset($page_seq) && print($page_seq) ?>

But why not just:
<?php @echo $page_seq; ?>
If it's not set, it's not gonna print anything either, why bother testing?

--
Ra*********@gmail.com

"Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
May 1 '07 #3

P: n/a
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.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
May 1 '07 #4

P: n/a
Rami Elomaa wrote:
Tyno Gendo kirjoitti:
>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; ?>

You could make this shorter ;)

<?php isset($page_seq) && print($page_seq) ?>

But why not just:
<?php @echo $page_seq; ?>
If it's not set, it's not gonna print anything either, why bother testing?
I'm using a custom error handler and the @ doesn't seem to be supressing
errors, don't know why. However, I would prefer to handle all errors
correctly rather than supressing as sometimes I've found in the past
that I've supressed some errors and then found it hard to locate what's
going wrong in a script whereas I would have been fully aware if I
hadn't supressed errors in the first place :S

I know trying to handle as many errors as possible might have a slight
performance hit perhaps, but nothing major.
May 2 '07 #5

P: n/a
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.
May 2 '07 #6

P: n/a
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.
js*******@attglobal.net
==================
May 2 '07 #7

P: n/a
"Tyno Gendo" <you@localhostwrote in message
news:fs******************************@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. :)

--
Ra*********@gmail.com

"Good tea. Nice house." -- Worf
May 2 '07 #8

P: n/a
Tyno Gendo <you@localhostwrote:
I'm using a custom error handler and the @ doesn't seem to be supressing
errors, don't know why.
/*. void .*/ function my_error_handler(
/*. int .*/ $errno,
/*. string .*/ $message,
/*. string .*/ $filename,
/*. int .*/ $lineno)
{
if( ($errno & error_reporting()) == 0 )
# Error disabled or silenced by @.
return;

/* bla bla */
}

Regards,
___
/_|_\ Umberto Salsi
\/_\/ www.icosaedro.it

May 2 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.