PHP echo and print Functions
⭐ PHP echo and print Functions
Both PHP echo and print Functions are used to output text, variables, HTML, and results to the browser.
They look similar, but there are a few differences.
1. PHP echo
Features:
-
Can output multiple values
-
Faster than
print -
No return value
-
Most commonly used
Example:
Echo multiple strings:
2. PHP print
Features:
-
Can output only one value
-
Slightly slower than
echo -
Returns 1 → can be used in expressions
Example:
Print cannot output multiple values:
Echo vs Print — Summary Table
| Feature | echo | |
|---|---|---|
| Outputs multiple items | ✔ Yes | ❌ No |
| Speed | Faster | Slightly slower |
| Return value | No | Yes (returns 1) |
| Usage | Very common | Less common |
| Parentheses allowed | Optional | Optional |
Using echo / print with Variables
Example:
Using echo / print with HTML
Using Concatenation (.)
Example Showing Both
When to Use What?
| Use Case | Best Option |
|---|---|
| Most PHP output | echo |
| Expression-based output | |
| Printing multiple things together | echo |
| Working with return values |
If you want, I can continue with the next topic:
👉 PHP Data Types
👉 PHP Strings
👉 PHP Operators
👉 PHP Constants
