| Recommend this page to a friend! |
| Info | Documentation | Reputation | Support forum | Blog | Links |
| Last Updated | Ratings | Unique User Downloads | Download Rankings | |||||
| 2025-11-29 (2 months ago) | Not yet rated by the users | Total: Not yet counted | Not yet ranked | |||||
| Version | License | PHP version | Categories | |||
| pretty-print 0.4.0 | Custom (specified... | 8.2 | Debug, Data types, PHP 8 |
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, summarized tensor views, and flexible output options – making it ideal for ML experiments, debugging, logging, and educational projects.
Installation composer require apphp/pretty-print Usage Note: When used in web (non-CLI) environments, output is automatically wrapped in <pre> to preserve spacing. In CLI, no wrapping is applied.
Global helper functions Print scalars/strings
pprint('Hello', 123, 4.56); // Hello 123 4.5600 Print multiple 1D rows aligned as a 2D table
pprint([1, 23, 456], [12, 3, 45]); // [[ 1, 23, 456], // [12, 3, 45]] Label + 2D matrix
pprint('Confusion matrix:', [[1, 23], [456, 7]]); // Confusion matrix: // [[ 1, 23], // [456, 7]] 2D tensor-style formatting
$matrix = [
[1,2,3,4,5],
[6,7,8,9,10],
[11,12,13,14,15],
]; pprint($matrix); // tensor([ // [ 1, 2, 3, 4, 5], // [ 6, 7, 8, 9, 10], // [11, 12, 13, 14, 15] // ]) Custom label instead of "tensor"
pprint($matrix, label: 'arr'); // arr([ // [ 1, 2, 3, 4, 5], // [ 6, 7, 8, 9, 10], // [11, 12, 13, 14, 15] // ]) 2D tensor-style formatting with summarization
$matrix = [
[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
]; pprint($matrix, headRows: 2, tailRows: 1, headCols: 2, tailCols: 2); // tensor([ // [ 1, 2, ..., 4, 5], // [ 6, 7, ..., 9, 10], // ..., // [21, 22, ..., 24, 25] // ]) 3D tensor with head/tail blocks (PyTorch-like)
$tensor3d = [
[[1,2,3],[4,5,6]],
[[7,8,9],[10,11,12]],
[[13,14,15],[16,17,18]],
]; pprint($tensor3d, headB: 1, tailB: 1, headRows: 1, tailRows: 1, headCols: 1, tailCols: 1); // tensor([ // [[1, ..., 3], // [4, ..., 6]], // // ..., // // [[13, ..., 15], // [16, ..., 18]] // ]) Postfix and prefix control
// No newline at the end (like Python's end="") pprint('Same line', end: ''); // Added newline at the end after printing pprint('Add line'); pprint('Add line', end: "\n"); // Added addedional 2 newlines at the end after printing pprint('Add 2 lines', end: "\n\n");
// Add a prefix at the start of the printed string pprint('Tabbed', start: "\t"); // Combine with end to avoid newline pprint('Prompted', start: '>>> ', end: ''); Print and then exit the script
ppd('Fatal error'); As an object use Apphp\PrettyPrint\PrettyPrint;
$pp = new PrettyPrint(); $pp('Hello', 42); // same as pprint('Hello', 42)
$tensor3d = [
[[1,2,3],[4,5,6]],
[[7,8,9],[10,11,12]],
[[13,14,15],[16,17,18]],
];
// Named options are supported $pp($tensor3d, headB: 2, tailB: 1, headRows: 1, tailRows: 1, headCols: 1, tailCols: 1);
// Label + 2D $pp('Metrics:', [[0.91, 0.02], [0.03, 0.88]]); Running tests
composer install
composer test
composer test:coverage 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. Options reference start: string. Prefix printed before the content. Example: pprint('Hello', ['start' => "\t"]). end: string. Line terminator, default to new line. Example: pprint('no newline', ['end' => '']); label: string. Prefix label for 2D/3D formatted arrays, default tensor. Example: pprint($m, ['label' => 'arr']). precision: int. Number of digits after the decimal point for floats. Example: pprint(3.14159, precision: 2) prints 3.14. 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. All options can be passed as:
trailing array: pprint($m, ['headRows' => 1, ...]) named args (PHP 8+): $pp($m, headRows: 1, ...) Defaults label: tensor precision: 4 headB / tailB: 5 headRows / tailRows: 5 headCols / tailCols: 5 Limits precision: max 10 headB / tailB / headRows / tailRows / headCols / tailCols: max 50 label: max length 50 characters (longer labels are truncated) positional args (MAX_ARGS): up to 32 positional 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.
Callable pretty-printer for PHP arrays with Python-like formatting. PrettyPrint is a small, zero-dependency PHP utility that formats numeric arrays in a clean, readable, PyTorch-inspired style. It supports aligned 2D tables, summarized tensor views, and flexible output options ? making it ideal for ML experiments, debugging, logging, and educational projects.
composer require apphp/pretty-print
Note: When used in web (non-CLI) environments, output is automatically wrapped in <pre> to preserve spacing. In CLI, no wrapping is applied.
Print scalars/strings
pprint('Hello', 123, 4.56);
// Hello 123 4.5600
Print multiple 1D rows aligned as a 2D table
pprint([1, 23, 456], [12, 3, 45]);
// [[ 1, 23, 456],
// [12, 3, 45]]
Label + 2D matrix
pprint('Confusion matrix:', [[1, 23], [456, 7]]);
// Confusion matrix:
// [[ 1, 23],
// [456, 7]]
2D tensor-style formatting
$matrix = [
[1,2,3,4,5],
[6,7,8,9,10],
[11,12,13,14,15],
];
pprint($matrix);
// tensor([
// [ 1, 2, 3, 4, 5],
// [ 6, 7, 8, 9, 10],
// [11, 12, 13, 14, 15]
// ])
Custom label instead of "tensor"
pprint($matrix, label: 'arr');
// arr([
// [ 1, 2, 3, 4, 5],
// [ 6, 7, 8, 9, 10],
// [11, 12, 13, 14, 15]
// ])
2D tensor-style formatting with summarization
$matrix = [
[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
];
pprint($matrix, headRows: 2, tailRows: 1, headCols: 2, tailCols: 2);
// tensor([
// [ 1, 2, ..., 4, 5],
// [ 6, 7, ..., 9, 10],
// ...,
// [21, 22, ..., 24, 25]
// ])
3D tensor with head/tail blocks (PyTorch-like)
$tensor3d = [
[[1,2,3],[4,5,6]],
[[7,8,9],[10,11,12]],
[[13,14,15],[16,17,18]],
];
pprint($tensor3d, headB: 1, tailB: 1, headRows: 1, tailRows: 1, headCols: 1, tailCols: 1);
// tensor([
// [[1, ..., 3],
// [4, ..., 6]],
//
// ...,
//
// [[13, ..., 15],
// [16, ..., 18]]
// ])
Postfix and prefix control
// No newline at the end (like Python's end="")
pprint('Same line', end: '');
// Added newline at the end after printing
pprint('Add line');
pprint('Add line', end: "\n");
// Added addedional 2 newlines at the end after printing
pprint('Add 2 lines', end: "\n\n");
// Add a prefix at the start of the printed string
pprint('Tabbed', start: "\t");
// Combine with end to avoid newline
pprint('Prompted', start: '>>> ', end: '');
Print and then exit the script
ppd('Fatal error');
use Apphp\PrettyPrint\PrettyPrint;
$pp = new PrettyPrint();
$pp('Hello', 42); // same as pprint('Hello', 42)
$tensor3d = [
[[1,2,3],[4,5,6]],
[[7,8,9],[10,11,12]],
[[13,14,15],[16,17,18]],
];
// Named options are supported
$pp($tensor3d, headB: 2, tailB: 1, headRows: 1, tailRows: 1, headCols: 1, tailCols: 1);
// Label + 2D
$pp('Metrics:', [[0.91, 0.02], [0.03, 0.88]]);
# install dev dependencies
composer install
# run test suite
composer test
# run tests with coverage (requires Xdebug or PCOV)
composer test:coverage
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.
All options can be passed as:
- trailing array: pprint($m, ['headRows' => 1, ...])
- named args (PHP 8+): $pp($m, headRows: 1, ...)
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.
| File | Role | Description | ||
|---|---|---|---|---|
| Aux. | Auxiliary script | |||
| Data | Auxiliary data | |||
| Data | Auxiliary data | |||
| Lic. | License text | |||
| Data | Auxiliary data | |||
| Data | Auxiliary data | |||
| Doc. | Documentation | |||
| / | src |
| File | Role | Description |
|---|---|---|
| |
Class | Class source |
| |
Aux. | Auxiliary script |
| |
Class | Class source |
| |
Class | Class source |
| / | tests |
| File | Role | Description |
|---|---|---|
| |
Class | Class source |
| |
Class | Class source |
| |
Class | Class source |
| |
Class | Class source |
| The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. |
| Version Control | Unique User Downloads | |||||||
| 100% |
|
| Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.
| Related pages |
| GitHub repo |