CodeBlanks Logo Icon CodeBlanks

Output with echo

PHP course · Beginner · Lesson 3/116

Learn to print text and variables to the screen with the echo statement.

What is echo?

echo is a language construct in PHP used to print output. It can print text, numbers or HTML.

The simplest form: echo "Hello"; — this prints Hello to the screen.

echo is not a function but a language construct; therefore it does not require parentheses. You can write echo("Hello"); but the parentheses are optional.

Every statement ends with a semicolon ;.

echo versus print

PHP has two common ways to produce output: echo and print. Both work similarly but have small differences.

echo returns no value and can take multiple parameters separated by commas: echo "a", "b";.

print always returns 1 and takes a single parameter. It can be used inside an expression.

Generally echo is preferred because it is slightly faster and more flexible.

Exercise Print Hello World

Write a PHP block. Use echo to print "Hello World"; add \n at the end of the line.

Show example solution
Write echo with "Hello World" in double quotes and add \n at the end.
Example solution:
<?php
// print the message
echo "Hello World\n";

Test yourself

  1. What does echo "Hello"; do?

    <?php echo "Hello"; ?>
    • Saves the word Hello
    • Prints Hello to the screen
    • Hides Hello
    • Opens the Hello file
    Show answer

    Prints Hello to the screen — Correct! echo prints Hello to the screen.

  2. Print text with echo

    <?php echo "____"; ?>
    Show answer
    <?php echo "Hello World"; ?>
  3. echo and print features

    echo
    Returns no value
    print
    Returns 1
    echo "a", "b"
    Prints two values

All lessons in Beginner