PHP Echo, Print & Output Functions Explained with Examples
Introduction
When you start programming in PHP, one of the first things you need to learn is how to display output on the screen.
In PHP, the most common ways to display information are using echo and print statements — also known as output functions.
These functions are used to:
Display text or variables
Output HTML content dynamically
Debug or show program results
Let’s explore these output functions in detail with examples.
What Are Output Functions in PHP?
Output functions are used to send data from the server to the browser.
They take PHP expressions (like strings or variables) and display them as part of the HTML page.
Common PHP Output Functions:
| Function | Purpose |
|---|---|
echo | Output one or more strings |
print | Output a string (and returns a value) |
print_r() | Output human-readable array/object data |
var_dump() | Output detailed variable info (type + value) |
Using echo in PHP
The echo statement is the simplest and fastest way to print data to the browser.
It can output text, numbers, HTML tags, or variables.
Syntax:
echo "text"; echo "text1", "text2", "text3"; // multiple strings separated by commas
Example 1: Simple Output
<?phpecho "Hello, World!"; ?>Output:
Hello, World!Example 2: Displaying Variables
<?php $name = "Arvinder"; $age = 25; echo "Name: $name <br> Age: $age"; ?> Output:
Name: Arvinder Age: 25Example 3: Using HTML with echo
<?php echo "<h2>Welcome to PHP Learning!</h2>"; echo "<p>This text is displayed using echo.</p>"; ?> Output:
HTML is rendered properly in the browser.
Notes:
echodoes not return any value.It can print multiple strings separated by commas.
It’s faster than
print.
Using print in PHP
The print statement is similar to echo, but it has two key differences:
It can only take one argument at a time.
It returns a value (1), so it can be used in expressions.
Syntax:
print "Hello World!";
Example 1: Simple Output
<?php print "Learning PHP is fun!"; ?>Output:
Learning PHP is fun!Example 2: Printing Variables
<?php
$name = "John";
print "My name is $name";
?>
Output:
My name is JohnExample 3: Using print in an Expression
<?php
$status = print "PHP Rocks!";
?>
Output:
PHP Rocks! Here, $status will store value 1 because print returns a value.
Difference Between echo and print
| Feature | echo | |
|---|---|---|
| Number of parameters | Multiple | One only |
| Return value | No | Returns 1 |
| Speed | Slightly faster | Slightly slower |
| Usage | Preferred for normal output | Used when expression result matters |
| Example | echo "Hello"; | print "Hello"; |
In short: Use echo for general output and print when you need a return value.
Other PHP Output Functions
print_r()
Used to print arrays or objects in a readable format.
Useful for debugging.
Example:
<?php$colors = array("Red", "Green", "Blue"); print_r($colors); ?>Output:
Array ( [0] => Red [1] => Green [2] => Blue )var_dump()
Displays detailed information (data type + value) about a variable.
Commonly used by developers to debug code.
Example:
<?php $age = 25; var_dump($age); ?> Output:
int(25) var_export()
Returns parsable string representation of a variable.
Example:
<?php $name = "PHP"; var_export($name); ?> Output:
'PHP'Combining PHP Output with HTML
PHP output functions can display HTML tags directly, allowing dynamic page creation.
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$name = "Arvinder";
echo "<h1>Welcome $name!</h1>";
print "<p>Today is " . date("l") . "</p>";
?>
</body>
</html>
Best Practices
- Prefer
echofor multiple outputs. - Use
printwhen you need to return a value. - Use
print_r()andvar_dump()for debugging only — not in production. - Combine PHP and HTML carefully to keep code clean.
- Always end PHP statements with
;.