Skip to content

Instantly share code, notes, and snippets.

@pematt
pematt / pretty.php
Last active December 14, 2023 00:07
Pretty print php objects. Need php 7.2 or newer.
<?php
// Returns a string containing the type, and value in prettyfied JSON, of the argument.
function pretty ($var) {
return gettype($var) . ' ' . json_encode(
$var,
JSON_UNESCAPED_SLASHES | // Don't escape forward slashes. stripslashes() could be used afterwards instead
JSON_UNESCAPED_UNICODE | // Print unicode characters insteas of their encoding "€" vs "\u20ac"
JSON_PRETTY_PRINT | // Nice layout over several lines, human readable
JSON_PARTIAL_OUTPUT_ON_ERROR | // Substitute whatever can not be printed