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!

 

Link to comment
Share on other sites

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.)

 

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.