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).