Introduction
In PHP, echo and print are used to display output to the browser. Both are commonly used to print text, variables, HTML content, and other data.
In simple terms:
echo and print are language constructs in PHP used to display output.
What is echo in PHP?
The echo statement is used to output text or variables to the screen. It is one of the most commonly used output statements in PHP.
- It can output one or more strings.
- It does not return any value.
- It is slightly faster than print.
- It can be used with or without parentheses.
Syntax of echo
echo "Hello World";
OR
echo("Hello World");
Example of echo
echo "Welcome to PHP Tutorial";
Output
Echo with Multiple Parameters
One special feature of echo is that it can print multiple values at once.
Example
$name = "John";
$course = "PHP";
echo "Student Name: ", $name, " Course: ", $course;
Output
In this example, echo prints multiple strings separated by commas.
What is print in PHP?
The print statement is also used to display output in PHP. It works very similarly to echo, but there are a few differences.
- It prints only one string at a time.
- It returns a value.
- It is slightly slower than echo.
- It can also be used with or without parentheses.
Syntax of print
print "Hello World";
Example of print
print "Welcome to PHP Programming";
Output
Print with Variables
The print statement can also display variables.
Example
$name = "Tony";
print "Student Name: " . $name;
Output
Here the dot (.) operator is used to concatenate strings.
Difference Between echo and print
Although echo and print look similar, they have several differences.
| Feature | echo | |
|---|---|---|
| Type | Language construct | Language construct |
| Return Value | No return value | Returns 1 |
| Speed | Faster | Slightly slower |
| Multiple Arguments | Yes | No |
| Usage | More commonly used | Less commonly used |
Example Using echo with Multiple Arguments
echo "Learning ", "PHP ", "is ", "easy.";
Output
This is not possible with print because print accepts only one argument.
Example Showing Return Value of print
One unique feature of print is that it returns a value (1). This allows it to be used in expressions.
Example
$result = print "Hello PHP";
echo $result;
Output
Here:
- print “Hello PHP” displays the text.
- It returns 1, which is stored in $result.
This feature is rarely used in real-world programming.
Performance Comparison
When comparing performance, echo is slightly faster than print because:
- echo does not return a value
- print returns 1
However, the difference in speed is very small and usually not noticeable in normal applications.
For most developers, the choice depends on coding style and preference.
When Should You Use echo?
Use echo when:
- You want to print multiple strings
- You want better performance
- You are writing normal output statements
Example:
echo "Welcome to ", "PHP Programming";
When Should You Use print?
Use print when:
- You need a return value
- You want to use it inside expressions
Example:
if (print "Hello") {
echo " PHP";
}
Output:
However, this usage is rare in real applications.
Real-Life Example
Consider a simple user profile page.
Example
$name = "Rahul";
$course = "BCA";
echo "User Profile
";
echo "Name: " . $name . "
";
print "Course: " . $course;
Output
Name: Rahul
Course: BCA
Here both echo and print are used to display information on the webpage.
Conclusion
Both echo and print are used to display output in PHP, and they perform very similar tasks. However, there are a few differences between them.
The echo statement is faster and supports multiple arguments, making it the most commonly used output statement in PHP.
On the other hand, print returns a value and accepts only one argument, which makes it slightly different.
Related PHP Tutorials
echo vs print – Objective Questions (MCQs)
Q1. Which of the following can output multiple arguments?
Q2. Which one returns a value?
Q3. Which is faster in execution?
Q4. Which can be used in expressions?
Q5. Which statement is correct?