How to inspect the contents of a variable
Short answer
var_dump() shows a value together with its type:
<?php
var_dump('42', 42, 42.0, null);Output:
string(2) "42"
int(42)
float(42)
NULLAn array in a readable form
print_r() is shorter but shows no types:
<?php
print_r(['name' => 'Anna', 'tags' => ['php', 'sql']]);Output:
Array
(
[name] => Anna
[tags] => Array
(
[0] => php
[1] => sql
)
)To the log instead of the page
On a live site visitors would see debug output. A second argument of true returns the text as a string — it can be written to the log:
<?php
$user = ['id' => 7, 'name' => 'Anna'];
$text = print_r($user, true);
echo strlen($text) > 0 ? 'ready for error_log()' : '';Output:
ready for error_log()