Code Utils
To make our code more readable and clean, we'll use following global functions, that will be placed in utils.php file.
Pretty print arguments, that works like Python's print().
/**
* Helper: format a value for array pretty-printing (Python-like)
* - Nested arrays are supported recursively
* - Integers are unquoted; other scalars are quoted and escaped
*/
function _pprint_format_for_array($value) {
if (is_array($value)) {
$formattedItems = array_map('_pprint_format_for_array', $value);
return '[' . implode(', ', $formattedItems) . ']';
}
if (is_int($value) || is_float($value)) {
return (string)$value;
}
if (is_bool($value)) {
return $value ? 'True' : 'False';
}
if (is_null($value)) {
return 'None';
}
// For other scalars/objects castable to string: quote and escape
return "'" . addslashes((string)$value) . "'";
}
/**
* Pretty print arguments
* @param ...$args
* @return void
*/
function pprint(...$args) {
$parts = [];
foreach ($args as $arg) {
if (is_array($arg)) {
// Format arrays like Python: [1, 2, 3] or ["a", "b", "c"] with nested arrays
$parts[] = _pprint_format_for_array($arg);
} elseif (is_bool($arg)) {
// Format booleans like Python: True/False
$parts[] = $arg ? 'True' : 'False';
} elseif (is_null($arg)) {
// Format null like Python: None
$parts[] = 'None';
} else {
// Convert to string
$parts[] = (string)$arg;
}
}
echo implode(' ', $parts) . PHP_EOL;
}Last updated