PHP Classes

How to use a PHP pretty print array class to display array variables in way that is readable by humans using the package Pretty Print: Output array contents using PyTorch style

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2025-11-29 (Yesterday) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
pretty-print 0.4.0Custom (specified...8.2Debug, Data types, PHP 8
Description 

Author

This package can output array contents using PyTorch style.

It can take an array variable value and outputs the array entry values in a way that can be readable by humans.

The package provides classes and a function that can format the output.

Currently, it supports:

- Aligned 2D tables

- Summarized tensor views

Picture of samik71
  Performance   Level  
Name: samik71 <contact>
Classes: 2 packages by
Country: Israel Israel
Age: ???
All time rank: 8385 in Israel Israel
Week rank: 195 Up2 in Israel Israel Up
Innovation award
Innovation award
Nominee: 1x

Instructions

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

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&#8209;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.

Documentation

Pretty Pprint

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.

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

# 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.

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.


  Files folder image Files (16)  
File Role Description
Files folder image.github (1 directory)
Files folder imagesrc (4 files)
Files folder imagetests (4 files)
Accessible without login Plain text file .php-cs-fixer.php Aux. Auxiliary script
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpcs.xml.dist Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (16)  /  .github  
File Role Description
Files folder imageworkflows (1 file)

  Files folder image Files (16)  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file php.yml Data Auxiliary data

  Files folder image Files (16)  /  src  
File Role Description
  Plain text file Formatter.php Class Class source
  Accessible without login Plain text file functions.php Aux. Auxiliary script
  Plain text file PrettyPrint.php Class Class source
  Plain text file Validator.php Class Class source

  Files folder image Files (16)  /  tests  
File Role Description
  Plain text file FormatterTest.php Class Class source
  Plain text file FunctionsTest.php Class Class source
  Plain text file PrettyPrintTest.php Class Class source
  Plain text file ValidatorTest.php 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.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0