Download the PHP package apphp/pretty-print without Composer
On this page you can find all versions of the php package apphp/pretty-print. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download apphp/pretty-print
More information about apphp/pretty-print
Files in apphp/pretty-print
Package pretty-print
Short Description Callable pretty-printer for PHP arrays with Python-like formatting.
License MIT
Homepage https://apphp.com
Informations about the package pretty-print
Pretty Pprint
Callable pretty-printer for PHP arrays with Python-like formatting. PrettyPrint is a small, zero-dependency PHP utility that formats arrays in a clean, readable, PyTorch-inspired style. It supports aligned 2D tables, 3D tensors, summarized tensor views, and flexible output options – making it ideal for ML experiments, debugging, logging, and educational projects.
Installation
Usage
Note: When used in web (non-CLI) environments, output is automatically wrapped in <pre> to preserve spacing. In CLI, no wrapping is applied. When you request a string to be returned (see return option), no auto-wrapping is applied.
Import functions
All examples below assume you have imported the helper functions from the Apphp\PrettyPrint namespace, for example:
or simply
Global helper functions
Print scalars/strings
Print 1D arrays
Compare two arrays with a difference matrix
Compare two arrays by printing both matrices (stacked) with colored cells
Label + 2D matrix
2D tensor-style formatting
Custom label instead of "tensor"
2D tensor-style formatting with summarization
3D tensor with head/tail blocks (PyTorch-like)
Select specific rows/columns
Print numeric-only column summaries
Postfix and prefix control
Return the formatted string instead of printing
Print and then exit the script
As an object
Objects with asArray() / toArray()
If you pass an object that exposes an asArray() or toArray() method, pprint / PrettyPrint will automatically convert it to an array before formatting:
If asArray() is not present but toArray() is, toArray() will be used instead.
Associative rows and strings in arrays/tensors
- Associative rows are normalized to positional values in 2D/3D formatting.
- String cells inside arrays/tensors are always printed with quotes (for example,
'engaged'). - Top-level scalar strings are unchanged and remain unquoted.
Running tests
Notes:
- Coverage drivers: You need Xdebug (xdebug.mode=coverage) or PCOV enabled for coverage reports. Without a driver, PHPUnit will warn and exit non‑zero.
- You can also run PHPUnit directly:
vendor/bin/phpunit.
Benchmark
A CLI benchmark script is available at benchmarks/benchmark.php for measuring matrix build/format time and peak memory usage.
Examples:
Notes:
- Presets:
small,10k,100k,1m. - Use
--dry-runfor huge shapes to avoid OOM. - The script skips materialization if total cells exceed
--max-cells.
Options reference
- start: string. Prefix printed before the content. Example:
pprint('Hello', ['start' => "\t"]). - end: string. Line terminator, default is double lines. Example:
pprint('no newline', ['end' => '']); - sep: string. Separator between multiple default-formatted arguments. Default is a new line. Examples:
pprint('A','B','C', sep: ', ', end: '')orpprint('X','Y', ['sep' => "\n", 'end' => '']). - label: string. Prefix label for 2D/3D formatted arrays, default
array. Example:pprint($m, ['label' => 'arr']). - precision: int. Number of digits after the decimal point for floats. Example:
pprint(3.14159, precision: 2)prints3.14. - short: bool. When
true, trims trailing float zeros and final dot (for example1.0000->1,2.5000->2.5). Default isfalse. - return: bool. When true, do not echo; return the formatted string instead (no
<pre>wrapping in web context). Example:$s = pprint($m, return: true);. - headB / tailB: ints. Number of head/tail 2D blocks shown for 3D tensors.
- headRows / tailRows: ints. Rows shown per 2D slice with ellipsis between.
- headCols / tailCols: ints. Columns shown per 2D slice with ellipsis between.
- colsSummary: bool. Adds a final per-column summary row for displayed columns in 2D/3D output; only
int/floatvalues are summed. - colsSummaryLabel: string. Visual label printed above the
colsSummarysummary row. Default is---totals---. - rowsSummary: bool. Adds an extra trailing per-row summary column in 2D/3D output; only
int/floatvalues are summed. - rowsSummaryLabel: string. Visual label printed in the first row for the
rowsSummarysummary column. Default is---totals---. - rowsOnly / colsOnly: int or string. Limit visible rows/columns.
- Single index:
3or'3'. - Range:
'2-4'(inclusive). - Sparse mix:
'1-2,5,9-11'→ indices1,2,5,9,10,11. Applied to 2D arrays and to each 2D slice of 3D tensors.
- Single index:
All options can be passed as:
- trailing array:
pprint($m, ['headRows' => 1, ...]) - named args (PHP 8+):
$pp($m, headRows: 1, ...)
Defaults
- label:
tensor - sep:
' ' - precision:
4 - short:
false - colsSummaryLabel:
---totals--- - rowsSummaryLabel:
---totals--- - headB / tailB:
5 - headRows / tailRows:
5 - headCols / tailCols:
5
Limits
- precision: max
10 - headB / tailB / headRows / tailRows / headCols / tailCols: max
50 - label: max length
50characters (longer labels are truncated) - positional args (MAX_ARGS): up to
32positional args are accepted; extras are ignored.
Positional policy:
- First arg can be a string label, number, or array.
- Exactly two positional args are allowed only for
string label, array. - Named/trailing options are applied only when the first arg is an array.