Echo and print in PHP

E
Trusted SSL from just $3.44
Getting Started with PHP
Variables in PHP

One of the most important things while programming is a way to output content on your screen. That content can be a simple string of text, but it can also be the output of a variable or the result of a complex mathematical outcome. In this tutorial we will cover how to use the echo and print functions by using PHP.

There are multiple functions for displaying content.
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 (also read our article about arrays in PHP).

While these function give the same result, there are some small, but important differences between the Echo() and Print() functions.

Echo and Print in PHP

  • 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.

Echo and print usage

Below a simple example of echo and print.

<?php
echo "In PHP you can either use the echo statement <br />";
print "or the print statement in order 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 in order 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";
?>

Echo

Technical details of the echo function.

SyntaxEcho ($strings)
ParametersExpression – One or more string expressions, separated by commas.
Return valueNo value is returned

Print

Technical details of the print function.

SyntaxPrint ($string)
ParametersExpression – The expression to output. Non-string values will be coerced to strings.
Return valueAlways 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.

Getting Started with PHP
Variables in PHP

Tutor Network

Share this

Learn PHP from A to Z