In PHP, echo and print are two commonly used language constructs for outputting data to the screen. They are similar but have slight differences in functionality and performance.
This tutorial covers:
Table of Contents
1. What is echo?
- echo is a PHP language construct used to output one or more strings.
- It does not return any value.
- It is faster than print because it does not return a value.
2. What is print?
- print is also a language construct used to output a string.
- It returns a value (always 1), making it slightly slower than echo.
3. Differences Between echo and print
Feature | echo | |
---|---|---|
Syntax | echo “text”; | print “text”; |
Return Value | None | Returns 1 |
Number of Arguments | Multiple arguments allowed | Single argument only |
Performance | Faster | Slightly slower |
4. Using echo with Examples
Example 1: Basic String Output
<?php echo "Hello, World!"; ?>
Output:
Hello, World!
Example 2: Output Multiple Strings
<?php echo "Hello, ", "World!", " How are you?"; ?>
Output:
Hello, World! How are you?
Example 3: Using Variables with echo
<?php $name = "Alice"; echo "Hello, $name!"; ?>
Output:
Hello, Alice!
Example 4: Using Concatenation with echo
<?php $greeting = "Hello"; $name = "Bob"; echo $greeting . ", " . $name . "!"; ?>
Output:
Hello, Bob!
5. Using print with Examples
Example 1: Basic String Output
<?php print "Hello, World!"; ?>
Output:
Hello, World!
Example 2: Using print with Variables
<?php $name = "Charlie"; print "Welcome, $name!"; ?>
Output:
Welcome, Charlie!
Example 3: Return Value of print
<?php $result = print "Hello!"; echo "\nReturn Value: $result"; ?>
Output:
Hello! Return Value: 1
6. Combining HTML with echo and print
Example 1: Embedding HTML in echo
<?php echo "<h1>Welcome to My Website</h1>"; echo "<p>This is a paragraph.</p>"; ?>
Output:
Welcome to My Website This is a paragraph.
Example 2: Embedding HTML in print
<?php print "<ul>"; print "<li>Item 1</li>"; print "<li>Item 2</li>"; print "</ul>"; ?>
Output:
- Item 1 - Item 2
Example 3: Mixing PHP and HTML
<!DOCTYPE html> <html> <head> <title>PHP Echo and Print</title> </head> <body> <?php echo "<h1>Hello, World!</h1>"; $name = "Dave"; print "<p>Welcome, $name!</p>"; ?> </body> </html>
7. Practical Examples
Example 1: Dynamic Greeting
<?php $hour = date("H"); if ($hour < 12) { echo "Good Morning!"; } elseif ($hour < 18) { echo "Good Afternoon!"; } else { echo "Good Evening!"; } ?>
Output:
Good Morning! (if it's before noon) Good Afternoon! (if it's between noon and 6 PM) Good Evening! (if it's after 6 PM)
Example 2: Display a Product List
<?php $products = ["Laptop", "Smartphone", "Tablet"]; echo "<h2>Available Products:</h2>"; echo "<ul>"; foreach ($products as $product) { print "<li>$product</li>"; } echo "</ul>"; ?>
Output:
Available Products: - Laptop - Smartphone - Tablet
Example 3: Conditional Output
<?php $isAdmin = true; echo $isAdmin ? "Welcome, Admin!" : "Welcome, User!"; ?>
Output:
Welcome, Admin! (if $isAdmin is true) Welcome, User! (if $isAdmin is false)
Best Practices
- Use echo for Outputting Multiple Strings:
echo "Part 1", " Part 2", " Part 3";
- Use print When a Return Value is Needed:
$result = print "This works!";
- Mix PHP and HTML Carefully:
echo "<h1>Hello</h1>";
- Use Concatenation for Complex Output:
echo "Hello, " . $name . "!";
Summary
Feature | echo | |
---|---|---|
Syntax | echo “text”; | print “text”; |
Speed | Faster | Slightly slower |
Return Value | None | Returns 1 |
Use Case | Output multiple strings | Output a single string and get a return value |
echo is generally preferred for its simplicity and performance, while print is useful when a return value is required.