PHP Basics: Difference between Echo and Print

Hey guys,

I am just wondering how Echo and Print are different.
I gather that they are both used to output results, but is one more apt for a particular situation than the other?

EDIT: While I have you guys here, maybe you can explain the difference between $_POST and $_GET :slight_smile:

Thanks

http://www.php.net/variables.superglobals

$_GET passes the data via a query string in the url

$_POST passes the data behind the scenes

Both are accessed:

$foo=$_POST[‘bar’];

$foo=$_GET[‘bar’];

Print is a more primitive form of echo.

Print always returns 1 (true) and can accept only one parameter, whereas echo doesn’t return a value and can accept multiple parameters that are then concatenated together.

See What is the difference between echo and print?

lol …and read many times …just remembering that
to add…print is function and echo is language construct…
(type of ?s that are asked in zend certification…)

They’re both language constructs actually. They don’t require parenthesis, and in fact when using print() in a conditional statement, using parenthesis for the print statement is likely to cause problems.

I don’t think it is fair to say one is more primitive than the other: they’re just different. Then again, that’s only my opinion. :slight_smile:

I agree that it was an unfair characterisation that I made against the print statement. It would be fairer to say that in my opinion, the echo statement is the more useful of the two on a day-to-day basis.

Be aware that with both methods a query string is passed. The only real difference (one that php doesn’t highlight) is that POST and GET actually have meaning in http.

The actual difference between GET and POST is that with GET the browser is allowed to assume that if it does multiple calls passing the same value that the same result will be returned whereas with POST the assumption is that the data on the server may be updated and so a second identical call may return different results.

ok seems like that book note was wrong …
any way i found this
Note: The print() function is not actually a function, so you are not required to use parentheses with it.

Tip: The print() function is slightly slower than echo().

print() function is not a function :smiley: sound whimsical but true

And the speed difference is even greater when you use echo with comma-separated strings, as opposed to concatenated strings. But even so, such minor speed situations are not the reason to use echo.

See: PHP Echo Vs Print: Which Should Developers Use?

My favorite part of that post is that at the beginning, they’re printing/echoing “Hello World”, but when they move to concatenation, they completely cut the spaces out of the equation, and are echoing “HelloWorld<br>”.

Mmmm, details.

:lol: