Jump to content

What's the difference between echo and print in for's expr3?


dilong

Recommended Posts

Hi!

I wonder why this code is working:

for($i=0; $i<5; print $i++);

while this code:

for($i=0; $i<5; echo $i++);

gives the error

PHP Parse error:  syntax error, unexpected T_ECHO, expecting ')'

After spending some hours at php.net I learned that both calls ('echo' and 'print') are not real functions, but I wonder why they behave so different in the same context?!

Also 'tricks' like

for($i=0; $i<5; (echo $i), $i++);

do not work. Is 'echo' just forbidden in that particular case?

 

Best regards!

 

You are correct, there are many ways to avoid that problem/bug or whatever this is.

I just noticed that behaviour and was wondering WHY this happen!

 

(Actually I was writing something like "for($i=0; $i<5; echo $vara, $varb, $i++);" and received that same error.)

 

Hm, does 'for' require the expression3 to have a return value at 'compile' time? This can't be!

 

But the answer seems to be correct! When I try to use 'unset' instead of 'echo' I receive:

syntax error, unexpected T_UNSET, expecting ')'

Funny. Is there a reason for this?

 

 

i suspect since print returns a numeric value, it is a valid value within the for(). but echo does not return a value and is therefore not valid

 

I wonder why this code is working:

for($i=0; $i<5; print $i++);

while this code:

for($i=0; $i<5; echo $i++);

gives the error

PHP Parse error:  syntax error, unexpected T_ECHO, expecting ')'

 

i was wondering what the difference was, why print seemed to work but echo did not, so i looked them up in the manual. i still don't know why i would ever put a print or an echo within the for parameters as was done in the example code.

i suspect since print returns a numeric value, it is a valid value within the for(). but echo does not return a value and is therefore not valid

 

I wonder why this code is working:

for($i=0; $i<5; print $i++);

while this code:

for($i=0; $i<5; echo $i++);

gives the error

PHP Parse error:  syntax error, unexpected T_ECHO, expecting ')'

 

i was wondering what the difference was, why print seemed to work but echo did not, so i looked them up in the manual. i still don't know why i would ever put a print or an echo within the for parameters as was done in the example code.

 

Meh,  not really worth finding out in my opinion.

 

Why does it say echo outputs 1 or more string, but for print it just says echo's 1 argument...

http://stackoverflow.com/questions/3599127/in-php-why-wasnt-echo-implemented-as-a-function-not-echo-vs-printf

Echo is not a function and it doesn't return a value like print. Print is a language construct too - does not require parenthesis.

 

Manual: echo - No value is returned. print - Returns 1, always.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.