Skip to content

Instantly share code, notes, and snippets.

@lithrel
lithrel / prettyVarExport.php
Last active April 27, 2021 07:34
var_export with short array notation, array alignement and custom indentation
<?php
function prettyVarExport($var, array $opts = [])
{
$opts = array_merge(['indent' => '', 'tab' => ' ', 'array-align' => false], $opts);
switch (gettype($var)) {
case 'array':
$r = [];
$indexed = array_keys($var) === range(0, count($var) - 1);
$maxLength = $opts['array-align'] ? max(array_map('strlen', array_map('trim', array_keys($var)))) + 2 : 0;