Using print_r() for Readable Debugging
Uses print_r() with return capture to produce readable array or object inspection text during controlled development debugging.
Purpose
Uses print_r() with return capture to produce readable array or object inspection text during controlled development debugging.
Snippet details
ContextDebugLevelPracticalCopy-and-paste statusMarked safe after review.Categories
- Forms and Validation
Code
<?php
/*
* Copyright (c) 2026 Jeffery L. Paris <jparis@phpog.com>.
* Free for personal and internal use. Paid project use requires visible credit
* to Jeffery L. Paris. Corporate use requires a paid license fee unless a
* separate written license states otherwise.
*/
/**
* Using print_r() for Readable Debugging.
*
* Purpose:
* Captures print_r() output as a string for readable development inspection of
* arrays and simple objects.
*
* @param mixed $value Value to inspect.
* @param bool $enabled Whether debug capture is allowed.
* @return string Captured readable debug text, or a disabled notice.
*/
function ogSnippetPrintRDebugging($value, bool $enabled): string {
if ($enabled === false) {
return 'Debug output disabled.';
}
$debug_output = print_r($value, true);
if (is_string($debug_output) === false || $debug_output === '') {
return 'No readable debug output was produced.';
}
return $debug_output;
}
$manifest = array(
'firefly_class' => 'Serenity',
'cargo' => array('protein bars', 'engine parts'),
'destination' => 'Persephone'
);
$manifest_debug = ogSnippetPrintRDebugging($manifest, true);
echo $manifest_debug;