dilong Posted January 29, 2011 Share Posted January 29, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/226065-whats-the-difference-between-echo-and-print-in-fors-expr3/ Share on other sites More sharing options...
l4nc3r Posted January 29, 2011 Share Posted January 29, 2011 That's odd. Not really sure why echo doesn't work in that context. But why would you need it to? You can just echo out $i in the for statement. Quote Link to comment https://forums.phpfreaks.com/topic/226065-whats-the-difference-between-echo-and-print-in-fors-expr3/#findComment-1167019 Share on other sites More sharing options...
dilong Posted January 29, 2011 Author Share Posted January 29, 2011 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.) Quote Link to comment https://forums.phpfreaks.com/topic/226065-whats-the-difference-between-echo-and-print-in-fors-expr3/#findComment-1167030 Share on other sites More sharing options...
BlueSkyIS Posted January 29, 2011 Share Posted January 29, 2011 print returns an integer, echo returns nothing. http://php.net/manual/en/function.print.php http://php.net/manual/en/function.echo.php Quote Link to comment https://forums.phpfreaks.com/topic/226065-whats-the-difference-between-echo-and-print-in-fors-expr3/#findComment-1167038 Share on other sites More sharing options...
dilong Posted January 29, 2011 Author Share Posted January 29, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/226065-whats-the-difference-between-echo-and-print-in-fors-expr3/#findComment-1167075 Share on other sites More sharing options...
ignace Posted January 29, 2011 Share Posted January 29, 2011 print returns an integer, echo returns nothing. http://php.net/manual/en/function.print.php http://php.net/manual/en/function.echo.php Care to elaborate why this is of importance to for()? Quote Link to comment https://forums.phpfreaks.com/topic/226065-whats-the-difference-between-echo-and-print-in-fors-expr3/#findComment-1167081 Share on other sites More sharing options...
BlueSkyIS Posted January 29, 2011 Share Posted January 29, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226065-whats-the-difference-between-echo-and-print-in-fors-expr3/#findComment-1167093 Share on other sites More sharing options...
phpSensei Posted January 29, 2011 Share Posted January 29, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/226065-whats-the-difference-between-echo-and-print-in-fors-expr3/#findComment-1167108 Share on other sites More sharing options...
Ninjakreborn Posted January 30, 2011 Share Posted January 30, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/226065-whats-the-difference-between-echo-and-print-in-fors-expr3/#findComment-1167225 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.