In the previous tutorial we learnt about PHP variables. In this tutorial we will show how to display these variables on a screen. In PHP there are two ways to output something on a screen: Echo and Print. Echo and Print are so called language constructors. This is why they can be used with or without parentheses like print(),echo().
Echo and Print are almost the same, with some slight differences. Below you can see those differences in detail.
The difference between Echo and Print
- Print is best used within expressions, since it returns a value of 1;
- Echo does not return any value.
- Echo is faster than print.
- Print takes only one argument.
- Echo can take multiple parameters.
Echo and Print examples
A simple example of echo and print:
<?php echo "In PHP you can either use the echo statement, <br />"; print "or the print statement, to output text.<br />"; ?>
The above example outputs the following in your browser:
In PHP you can either use the echo statement, or the print statement, to output text.
You can combine multiple strings by using a “.” (dot).
<?php $str1 = "In PHP you can glue"; $str2 = $str1 . " strings together using a dot"; ?>
|Parameters||Expression – One or more string expressions, separated by commas.|
|Return value||No value is returned|
|Parameters||Expression – The expression to output. Non-string values will be coerced to strings.|
|Return value||Always returns 1.|