PHP 5 echo and print Statements

In PHP there are two basic ways to get output: echo and print.

In this tutorial we use echo (and print) in almost every example. So, this chapter contains a little more info about those two output statements.

Echo is not actually a function (it is a language construct), so you are not required to use parentheses with it. echo (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function. Additionally, if you want to pass more than one parameter to echo, the parameters must not be enclosed within parentheses.

PHP echo and print Statements

There are some differences between echo and print:

  • Echo - can output one or more strings
  • Print - can only output one string, and returns always 1
  • The PHP echo Statement

    The echo statement can be used with or without parentheses: echo or echo().

    Display Strings

    The following example shows how to output text with the echo command (notice that the text can contain HTML markup):

    Example -

    The PHP print Statement

    The print statement can be used with or without parentheses: print or print().

    Display Text

    The following example shows how to output text with the print command (notice that the text can contain HTML markup):

    Example -

    Display Variables

    The following example shows how to output text and variables with the print statement:

    Example -