PHP echo and print Functions

PHP echo and print – Complete Beginner Guide With Examples
Introduction to echo and print in PHP
In PHP, echo and print are language constructs used to display output to the browser. Whenever you want to show text, variables, HTML content, or calculation results, you use echo or print.
They are among the most commonly used statements in PHP programming.
Example:
This displays:
Understanding the difference between echo and print is important for beginners.
What is echo in PHP?
echo is a PHP language construct used to output data to the browser.
Basic Syntax:
Example:
Output:
What is print in PHP?
print is also a PHP language construct used to output data.
Basic Syntax:
Example:
Output:
Key Differences Between echo and print
Although both are used to display output, they are slightly different.
| Feature | echo | |
|---|---|---|
| Returns Value | No | Yes (returns 1) |
| Can Print Multiple Values | Yes | No |
| Slightly Faster | Yes | Slightly Slower |
| Parentheses Required | No | No |
In most cases, developers prefer echo.
echo Can Output Multiple Values
echo can print multiple strings separated by commas.
Output:
print cannot do this.
Using echo and print with Variables
Example using echo:
Example using print:
Both display variable values correctly.
Combining Text and Variables
You can combine strings and variables using:
Concatenation Operator (.)
Double Quotes (Variable Interpolation)
Note:
Double quotes allow variable expansion
Single quotes do not
Wrong:
Output:
Using HTML Inside echo and print
You can include HTML tags.
This displays a heading.
You can also mix PHP and HTML:
echo and print with Calculations
Output:
With print:
Output:
print Returns a Value
The main difference is that print returns 1.
Example:
Output:
Because print returns 1.
echo does not return anything.
Using echo in Loops
Output:
Short echo Tag
PHP also provides a shorthand syntax:
This works like:
Short echo is commonly used in templates.
When to Use echo vs print?
Use echo when:
Printing multiple values
Writing large output
Slightly better performance matters
Use print when:
You need a return value
Using it inside expressions
However, most developers prefer echo.
Common Beginner Mistakes
Forgetting Semicolon
Must end with:
Using Single Quotes Incorrectly
Variables don’t work inside single quotes.
Mixing Concatenation Improperly
Wrong:
Correct:
Performance Difference
echo is slightly faster than print because:
It does not return a value
It is simpler internally
But in small projects, the difference is negligible.
Real-World Example – Dynamic Web Page
Used in:
Login systems
Dashboards
Dynamic content
E-commerce websites
Security Tip When Using echo
When displaying user input:
Use:
This prevents XSS attacks.
Never directly output raw user data.
Summary Table
| Feature | echo | |
|---|---|---|
| Faster | Yes | Slightly slower |
| Multiple Arguments | Yes | No |
| Returns Value | No | Yes (1) |
| Most Used | Yes | Less common |
FAQs About PHP echo and print
1. What is the difference between echo and print?
echo does not return a value and can output multiple strings. print returns 1 and outputs only one string.
2. Which is faster, echo or print?
echo is slightly faster than print.
3. Can echo print variables?
Yes, echo can display variables and expressions.
4. Can print return a value?
Yes, print always returns 1.
5. Which one should beginners use?
Beginners should mostly use echo because it is more flexible and commonly used.
Conclusion
echo and print are essential PHP output statements. They allow you to display text, variables, calculations, and HTML content dynamically.
Key points:
echo is faster and more flexible
print returns a value
Both are easy to use
echo is more commonly preferred
Understanding these basics is important before learning:
Mastering output statements is the first step toward building dynamic PHP websites.
