agyanadda@gmail.com

PHP echo and print

PHP Basics 1 min read Updated Dec 1, 2026 Topic 5 of 36

echo and print

Both send text to the browser:

echo 'Hello';
echo 'A', 'B', 'C';   // echo accepts several values
print 'Hello';         // print accepts one value

echo is slightly faster and used most often.

printf — formatted output

printf('Total: ₹%.2f', 1234.5); // Total: ₹1234.50

%s = string, %d = integer, %.2f = number with 2 decimals.

Debugging output

Function Shows
print_r($arr) readable array or object
var_dump($x) type and value — best for debugging
var_export($x) valid PHP code

Wrap them in <pre> for neat formatting.

HTML in echo

echo '<h2>Title</h2>'; — the browser renders it as HTML.

Example

Example — Output and debugging functions PHP
<?php
$course = 'PHP';
$lessons = 36;
$price = 2499.5;
$skills = ['Variables', 'Arrays', 'MySQL'];

echo "<h3>$course Course</h3>";
printf('%s has %d lessons and costs ₹%.2f<br>', $course, $lessons, $price);

echo '<pre>';
print_r($skills);
var_dump($price, true);
echo '</pre>';

Output: a heading "PHP Course", then "PHP has 36 lessons and costs ₹2499.50", then Array ( [0] => Variables [1] => Arrays [2] => MySQL ), then float(2499.5) and bool(true).

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which function shows both the type and the value of a variable?

Exercise 2 True / False

echo can output several values separated by commas.

Exercise 3 Fill in the Blank

In printf, the placeholder %___ is used for a whole number.

Frequently Asked Questions

dd() means 'dump and die' — Laravel's helper that prints a variable nicely (like var_dump) and stops the script.