Why do print and echo behave differently in a "for" loop [duplicate]
Asked Answered
H

1

7

If I use print in this code:

<?php
for($i = 1; $i <= 3; print $i . "\n") {
  $i++; 
}   
?>

I see as output this:

2

3

4

But when I use echo the code doesn't work:

<?php
for($i = 1; $i <= 3; echo $i . "\n") {
  $i++; 
}   
?>

I see this error:

PHP Parse error: syntax error, unexpected 'echo' (T_ECHO), expecting ')' in /media/datos/xampp/htdocs/temp/1.php on line 3

My question is:

  • Why can I use print as a third expression in a for loop, but cannot when using echo and why do they behave differently from each other?

References:

Hygienist answered 8/6, 2015 at 17:37 Comment(22)
why would you echo or print there if you can in the loop?Roughdry
Probably because print returns a value; while echo doesn't return any value.... and an expression requires a valueElongation
@MarkBaker print is also a language construct.Armed
@Roughdry I was writing a example for a student, to see how you can use the expressions in for sentenceHygienist
@MarkBaker php.net/printArmed
@MarkBaker Directly from the documentation print is not actually a real function (it is a language construct)Armed
@JonStirling if you have $i++ as thrid expresion it don't return a valueHygienist
@AdrianCidAlmaguer $i++ does give a value.... as do all operators An operator is something that takes one or more values (or expressions, in programming jargon) __and yields another value (so that the construction itself becomes an expression)__.Elongation
@MarkBaker I test this: function a() { //just test; } for($i = 1; $i <= 3; a()) { $i++; } and it works and don't return any valueHygienist
in the same way, this is valid print print 'hello'; while this is invalid echo echo 'hello'; for the same reason, echo has not return valueAdellaadelle
@AdrianCidAlmaguer - Any user-defined function without an explicit return will return a null, that's a valid value in PHP, print returns an integer, which is a valid value..... echo returns void, which isn't a valueElongation
var_dump(print("hello")); return 1Buell
so, what's the question really about; a solution to fix code, or more of "why is it reacting that way and want to why print and echo behave differently?..."Besmirch
@Fred-ii- the second "why is it reacting that way?..." when you use print all is okHygienist
@KarolyHorvath Is just a example, to see when the third expression is executedHygienist
@AdrianCidAlmaguer Well, that's not my dv. I believe I improved on the question by editing it. If someone later visited the question and saw what the actual and precise reason was to find out the "why they react differently", then that could be a reason for the dv. You're more than welcome to perform a rollback. Edit: One finding I did notice, was https://mcmap.net/q/126248/-what-39-s-the-difference-between-echo-print-print_r-and-var_dump-in-php if that helps any.Besmirch
"echo (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function" -- php.net/echoHeisler
@AdrianCidAlmaguer Something just came to mind. You state using "\n" but in your original question you have those numbers listed one of top of each other, as if on new lines. \n will not give you those hard returns, but rather spaces. This will only appear as new lines if you're writing to a file. Is there a particular reason why you're using "\n" rather than a space " "? Is there a new reason for this that you haven't completely shared? Unless those numbers on top like that, was a mistake and you meant to write it as in a single line 2 3 4?Besmirch
@Fred-ii- I run php to test from the command line in linux like php 1.php in this case to see the new line I use "\n"Hygienist
@Fred-ii-: That is not true. You are thinking of how your web browser will not render newlines as such when translating HTML markup to the screen, because only the various HTML constructions can do that (e.g. <br/>). That has nothing to do with PHP. Try echoing "\n" in PHP through a web request, then hit "view source" in your browser.Viol
How can such a well documented question be marked as a duplicate of of a 1 phrase question??? +1 for this one here ...Truncated
@Truncated this was a large conversation, and now I see that some comments was deleted, but as I say this is the life in StackOverflow ;-)Hygienist
N
6

Expression. print() behaves like a function in that you can do: $ret = print "Hello World"; And $ret will be 1. That means that print can be used as part of a more complex expression where echo cannot. An example from the PHP Manual:

$b ? print "true" : print "false";

Some part of my answer are part of below answer. I think this is the answer of your question. Most important part is print() behaves like a function

see this answer: https://mcmap.net/q/116747/-how-are-echo-and-print-different-in-php-duplicate

What about echo:

Note: Because this is a language construct and not a function, it cannot be called using variable functions.

see notes part on this page: https://www.php.net/manual/en/function.echo.php

Nerty answered 8/6, 2015 at 18:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.