PHP Echo vs Print vs print_r() – Complete Guide with Examples (2026)

PHP Echo vs Print vs print_r() – Complete Guide with Examples (2026)

In PHP, displaying output is one of the most important parts of development. Whether you are building a simple program or a dynamic website, output handling plays a key role.

If you are new to PHP, you should first understand PHP basics and string functions before moving to advanced topics like output handling.

PHP echo vs print vs print_r comparison
Comparison of PHP echo, print, and print_r()


3 Ways to Display Output in PHP

  • echo – Fast and commonly used
  • print – Returns a value
  • print_r() – Used for arrays and debugging

Before learning this, you can also read hidden PHP features and tricks to improve your coding skills.


Echo Statement in PHP


<?php
echo "Hello, Rays Coding!";
?>

Echo is the most commonly used output statement. It is fast and can display multiple values.


Print Statement in PHP


<?php
print "Hello from PHP!";
?>

Print works like echo but returns a value, which allows it to be used in conditional statements.


print_r() Function in PHP


<?php
$arr = array("PHP", "HTML", "CSS");
print_r($arr);
?>

print_r() is mainly used for debugging arrays and objects. It helps developers understand data structures.


Key Differences

  • echo is fastest
  • print returns value
  • print_r() is used for debugging

Real Example


<?php
echo "<h2>Welcome</h2>";
print "Login Successful";
$data = array("Name"=>"Amit");
print_r($data);
?>

Frequently Asked Questions

What is the difference between echo and print?

Echo is faster and supports multiple values, while print returns a value and works in conditions.

When should I use print_r()?

Use print_r() for arrays and debugging complex data structures.


Conclusion

Echo, print, and print_r() are essential tools in PHP. Use echo for general output, print for conditions, and print_r() for debugging.

Post a Comment

Previous Post Next Post