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

true && print()

P: n/a
Hi.

function printx($str) {
echo $str;
}

true && printx("foo");
true && print("bar");
Why do I see "foo", but not "bar"?

According to the manual, print and echo are language constructs, not
functions. That seems to be the reason why they don't behave as
expected, but what I'm looking for is an explanation. Is there a
rationale for not executing print() in this context?

tia,
stefan
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Stefan Weiss <sp******@foo.at> wrote:
Hi.

function printx($str) {
echo $str;
}

true && printx("foo");
true && print("bar");
Why do I see "foo", but not "bar"?

According to the manual, print and echo are language constructs, not
functions. That seems to be the reason why they don't behave as
expected, but what I'm looking for is an explanation. Is there a
rationale for not executing print() in this context?


You GAVE the explanation. What you have written there are expressions.
printx is a function which returns a value (implicitly), and can thus be
used in an expression. "print" is a statement; it does not return a value,
and thus cannot participate in expressions.

C has made us all lazy; people aren't aware of the difference between an
expression and a statement. Consider that you are asking to do the same
thing as this in C:

1 && while( x == 3 ) {
dowhat(x);
}
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jul 17 '05 #2

P: n/a
Tim Roberts wrote:

You GAVE the explanation. What you have written there are expressions.
printx is a function which returns a value (implicitly), and can thus be
used in an expression. "print" is a statement; it does not return a value,
and thus cannot participate in expressions.

C has made us all lazy; people aren't aware of the difference between an
expression and a statement. Consider that you are asking to do the same
thing as this in C:

1 && while( x == 3 ) {
dowhat(x);
}


Actually, that's the main difference between print and echo. Print CAN
be used in expressions while echo can't. Print does have a return value
, and behaves like a normal function, despite it not actually being one.
See the last paragraph in the manual, which links to this faq:
http://www.faqts.com/knowledge_base/...l/aid/1/fid/40

In response to the OP, the code works just fine for me. It prints out
"foobar".
Jul 17 '05 #3

P: n/a
On 2005-06-17 08:08, kicken wrote:
Actually, that's the main difference between print and echo. Print CAN
be used in expressions while echo can't. Print does have a return value
, and behaves like a normal function, despite it not actually being one.
See the last paragraph in the manual, which links to this faq:
http://www.faqts.com/knowledge_base/...l/aid/1/fid/40
Yes, I am aware of the difference between print and echo, that is why I
used print in my example.
In response to the OP, the code works just fine for me. It prints out
"foobar".


Very interesting. I have tried the same script on several web servers
now, with the following results:

[SuSE 9.2, Apache/2.0.50]
This is localhost, running PHP 4.3.8
foobar

[SuSE 9.2, Apache/2.0.50]
This is localhost, running PHP 5.0.4
foobar

[Debian Sarge, Apache/2.0.53]
This is www.foo.at, running PHP 5.0.4
foobar

[Debian Sarge, Apache/1.3.33]
This is akira.foomatic.at, running PHP 5.0.4
foo
Huh.

The only interesting difference that I can see is that the last server
is running Apache 1 instead of Apache 2.
cheers,
stefan
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.