One of the most important things while programming is a way to output things on the screen. Those “things” can be a simple string of text, but also the content of a variable or the result of a complex mathematical outcome. In this tutorial we will cover how to display text on your screen in PHP.
There are multiple functions for outputting. Either you can use Echo() or Print(), which does the exact same thing. A third way to output is by using Print_r(), however this is meant for outputting arrays (read our article about arrays in PHP).
While having the same result there are a few small differences between Echo() and Print().
A closer look at Echo and Print
- Print always returns the value 1. Therefore it is best to use in expressions.
- The function Echo does not return any value.
- Echo is faster than Print.
- Print only requires one argument.
- Echo has multiple parameters.
Examples of use:
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.|
Now that you know how to output strings with the Echo and Print functions in PHP it is time to learn how to store those strings. You will learn this in the variables and data types article.