Introduction to echo and print in PHP
There are two primary ways to produce output in PHP: echo and print.
Both are mainly used for outputting strings, but they differ slightly in syntax and performance. This section explains these differences as well as important considerations when using each statement. Understanding these distinctions can offer several advantages to PHP developers.
Note:
PHP's echo and print can output not only strings but also expressions of various data types such as numbers, arrays, and objects, by converting them to strings.
echo vs. print Comparison
While both echo and print output expressions as strings in PHP, they have subtle differences. The table below summarizes their key similarities and differences.
| Comparison Item | echo |
print |
|---|---|---|
| Purpose | Outputs expressions as strings | Outputs expressions as strings |
| Number of Arguments | Can output multiple arguments separated by commas (,) simultaneously |
Cannot output multiple arguments separated by commas (,) simultaneously |
| Return Values | None | Always returns int(1) |
| Performance | Only minor differences | Only minor difference |
| Developer Preference | Preferred due to greater flexibility and no limit on the number of arguments | Less preferred because it accepts only a single argument |
Detailed Examples Comparing echo and print
Let's take a closer look at the differences between echo and print by examining specific cases for each comparison item.
Number of Arguments
| Comparison Item | echo |
print |
|---|---|---|
| Number of Arguments | Can output multiple arguments separated by commas (,) simultaneously |
Cannot output multiple arguments separated by commas (,) simultaneously |
echo allows multiple arguments separated by commas to be output at once:
$name = 'John';
$age = 25;
echo 'My name is ', $name, ' and I am ', $age, ' years old.';
// Output: My name is John and I am 25 years old.
print does not support multiple arguments separated by commas and will cause a parse error:
$name = 'John';
$age = 25;
print 'My name is ', $name, ' and I am ', $age, ' years old.';
// Parse error: syntax error, unexpected ','
echo and print work fine if you use string concatenation (the string operator .) instead of multiple arguments:
$name = "John";
$age = 25;
print "My name is " . $name . " and I am " . $age . " years old.";
// Output: My name is John and I am 25 years old.
echo "My name is " . $name . " and I am " . $age . " years old.";
// Output: My name is John and I am 25 years old.
Note:
This does not mean print cannot output multiple variables at once; it simply cannot accept multiple arguments separated by commas.
Return Value
| Comparison Item | echo |
print |
|---|---|---|
| Return Value | None | Always returns int(1)
|
echo does not have a return value, attempting to assign it to a variable will result in a parse error:
$result = echo "Hello, World!";
// Parse error: syntax error, unexpected 'echo' (T_ECHO)
// Parsing error: echo does not return a value.
print returns int(1), so it can be assigned to a variable:
$result = print("Hello, World!");
var_dump($result); // int(1)
Performance
$start = microtime(true);
for ($i = 0; $i < 100000; $i++) {
echo 'Hello, world!';
}
$end = microtime(true);
$echo_time = $end - $start;
$start = microtime(true);
for ($i = 0; $i < 100000; $i++) {
print 'Hello, world!';
}
$end = microtime(true);
$print_time = $end - $start;
echo "echo_time: $echo_time\n";
echo "print_time: $print_time\n";
// Second run
// Third run
From these results, we can see that the performance difference between echo and print is negligible. Both are language constructs used to output data to the screen, with only minor differences in return behavior, argument handling, and speed.
Note:
Don't expect a big speed difference between echo and print!
This example measures performance under extreme conditions. In real-world scenarios, any speed difference is practically unnoticeable.
Why Understanding the Differences Between echo and print Matters
- Flexible Output:
Knowing when to use
echoversusprintallows you to adapt to different output needs. For example,echosupports multiple arguments and works well when combining strings with HTML elements. - Performance Awareness:
While the performance difference is minimal, having the option to choose between
echoandprintgives you control when optimizing for output in high-iteration scenarios. - Consistent Coding Style: Developers can choose the statement that best aligns with their coding preferences. Since each has slightly different syntax, understanding both helps maintain consistency and clarity in your codebase.
- Better Code Comprehension:
Recognizing the nuances between
echoandprintimproves your ability to read and understand different PHP codebases, especially when working on team projects or contributing to open-source code.
Understanding these distinctions gives PHP developers more control, clarity, and flexibility in their code.
In practice, many developers prefer echo for its broader compatibility and support for multiple arguments.