echo vs print vs print_r vs var_dump in PHP
Short answer
echo outputs text and is what you want in templates. print_r and var_dump are debugging tools — var_dump is the one that shows types, which is usually the information you actually need.
#echo and print
echo "Hello";
echo "a", "b"; // echo takes multiple arguments
print "Hello"; // returns 1, so it can sit in an expressionUse echo. It is marginally faster and takes multiple arguments. print returning a value is a curiosity that almost never matters.
In templates, use the short form:
<h1><?= htmlspecialchars($title) ?></h1>#print_r and var_dump
$data = ["count" => 3, "ok" => true, "score" => "5"];
print_r($data);
// Array ( [count] => 3 [ok] => 1 [score] => 5 )
var_dump($data);
// array(3) {
// ["count"]=> int(3)
// ["ok"]=> bool(true)
// ["score"]=> string(1) "5"
// }print_r renders true as 1 and "5" as 5 — exactly the distinctions you are usually debugging. var_dump shows the type and length, so it is the better default.
#Make it readable in a browser
echo '<pre>';
var_dump($data);
echo '</pre>';Or capture print_r output as a string with its second argument:
$text = print_r($data, true);
error_log($text);