PHP echo and print Statements

There are two ways to get the output in PHP – echo, and print.

echo and print statements in PHP are somehow similar but have some differences as well. echo statement does not have a return value while print has a return value of 1. echo can have multiple parameters whereas print can have one parameter.

Note: echo statement works faster than print

Example of echo

In this example, we have used the echo statement to display two strings.

<?php
    echo "Hello World <br />";
    echo "Hello", "Everyone";
?>

Output:

Hello World
Hello Everyone

Notice in the above example, that the HTML element <br /> contained in the string will be rendered as an HTML element. So there would be a line break after the string Hello World.

The echo statement can also display the value of a variable.

<?php
    $num1 = 4;
    echo $num1;
?>

Output:

4

We can also concatenate a variable with a string. Concatenation in programming means interconnecting different types of values.

Example

<?php
    $num1 = 4;
    echo "The number is " . $num1;
?>

Output:

The number is 4

Note: In PHP the dot operator(.) is used to concatenate two different data types.

The print statement

The print statement can be used with parenthesis or without it.

Example

<?php
    print "Hello World <br />";
    print("Hello Everyone");
?>

Output:

Hello World
Hello Everyone

Just like the echo statement, the print statement can also be used to display the value of a variable.

Example

<?php
    $num1 = 4;
    $message = "The number is ";
    print $message . $num1;
?>

Output:

The number is 4