PHP echo and print Statements

By Shahnail Khan Last updated : November 30, 2023

In PHP, both the echo and print statements are fundamental lines of code which is used to output data to the browser. Let's understand these statements in detail.

What are is echo and print Statement in PHP?

In PHP, both echo and print are language constructs used to output data. They are used to display strings, variables, or a combination of both on a web page. The primary difference lies in their syntax and the values they return.

PHP echo Statement

The echo statement is one of the most commonly used output constructs. It can take multiple parameters and doesn't have a return value.

Let's understand the echo statement by examples given below:

PHP echo Statement Example 1: Basic Text Output

<!DOCTYPE html>
<html>
<body>

<?php
    // Using echo to display text messages
    echo "<h1>Enjoy learning PHP with IncludeHelp.</h1>";
    echo "<p><i>Welcome to the world of PHP!!</i></p>";
?>

</body>
</html>

Output:

php echo example 1

In this example, the echo statement is used to display the text within an HTML document.

PHP echo Statement Example 2: Printing Variables Content

<!DOCTYPE html>
<html>
<body>

<?php
    // Using echo to display the content of a variable
    $favoriteColor = "blue";
    echo "My favorite color is " . $favoriteColor ;
    $add2nos= 13 +14;
    echo" <br> The sum of 13&14 is: ". $add2nos;
?>

</body>
</html>

Output:

php echo example 2

Here, the echo statement is used to display the content of the $favoriteColor and $add2nos variables, resulting in the display of "My favorite color is blue" and "27" respectively.

PHP Print Statement

Similar to echo, the print statement is used for displaying the content. However, it always returns 1, making it slightly slower than echo. Its syntax is more rigid, allowing only one parameter. It means that we can't write the print statement as:

print "Include", "Help"; 

This will result in syntax errors as the print statement doesn't allow multiple parameters.

PHP print Statement Example 1: Basic Text Output

<!DOCTYPE html>
<html>
<body>

<?php
    // Using print to display a simple text message
    print "<h1>Printing with PHP!</h1><p><i>Welcome to IncludeHelp</i></p>";
?>

</body>
</html>

Output:

php print example 1

In this example, the print statement is used to display the text within an HTML document.

PHP print Statement Example 2: Displaying Variables Content

<!DOCTYPE html>
<html>
<body>

<?php
    // Using print to display the content of a variable
    $favoriteNumber = 7;
    print "My favorite number is: " . $favoriteNumber;
?>

</body>
</html>

Output:

php print example 2

Here, the print statement is used to display the content of the $favoriteNumber variable, resulting in the display of "My favorite number is: 7".

Difference Between echo and print in PHP

The primary differences between echo and print in PHP are:

Sr. No.PHP echo StatementPHP print Statement
1 echo has no return value. print always returns 1.
2 echo can take multiple parameters. print can only take one parameter.

How is echo Faster Than print in PHP?

The execution time variance between the echo and print functions for smaller operations is insignificant. However, the execution time of an echo statement is less as it doesn't return a value. Since print returns 1, it involves an additional step, hence it takes more time to execute as compared to echo.

PHP echo and print Statements FAQs

1. Which statement should I use for general output?

Use echo for general output due to its simplicity, speed, and ability to handle multiple arguments.

2. Which statement is faster, echo, or print?

echo is generally considered faster due to its lack of a return value, making it slightly more efficient in terms of execution time.

3. Can I use multiple parameters with the print statement?

No, the print statement only allows one parameter. Unlike echo, it has a more rigid syntax and doesn't support multiple values.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.