PHP code example of apphp / pretty-print

1. Go to this page and download the library: Download apphp/pretty-print library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

apphp / pretty-print example snippets


use function Apphp\PrettyPrint\pprint;
use function Apphp\PrettyPrint\pp;
use function Apphp\PrettyPrint\ppd;

use function Apphp\PrettyPrint\{pprint, pp, ppd, pdiff, pcompare};

pprint('Hello', 123, 4.56);            
// Hello 123 4.5600

pprint([1, 23, 456], [12, 3, 45]);
// [1, 23, 456]
// [12, 3, 45]

$a = [
    [1, 2, 3],
    [4, 5, 6],
];

$b = [
    [1, 9, 3],
    [0, 5, 7],
];

pdiff($a, $b);
// [[1, '-', 3],
//  ['-', 5, '-']]

$a = [
    [1, 2, 3],
    [4, 5, 6],
];

$b = [
    [1, 9, 3],
    [0, 5, 7],
];

pcompare($a, $b);
// prints $a above $b
// - equal cells are green
// - different/missing cells are red
// In CLI: ANSI colors; in web: <span style="color: ..."> inside <pre>

pprint([[1, 23], [456, 7]], label: 'Confusion matrix:');
// Confusion matrix:([
//   [  1, 23],
//   [456,  7]
// ])

$matrix = [
    [1,2,3,4,5],
    [6,7,8,9,10],
    [11,12,13,14,15],
];
pprint($matrix);
// array([
//   [ 1,  2,  3,  4,  5],
//   [ 6,  7,  8,  9, 10],
//   [11, 12, 13, 14, 15]
// ])

pprint($matrix, label: 'arr');
// arr([
//   [ 1,  2,  3,  4,  5],
//   [ 6,  7,  8,  9, 10],
//   [11, 12, 13, 14, 15]
// ])

$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);
// array(5x5)([
//   [ 1,  2, ...,  4,  5],
//   [ 6,  7, ...,  9, 10],
//   ...,
//   [21, 22, ..., 24, 25]
// ])

$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);
// array(3x2x3)([
//  [[1, ..., 3],
//   [4, ..., 6]],
//  ...,
//  [[13, ..., 15],
//   [16, ..., 18]]
// ])

$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, 26, 27, 28, 29, 30, 31, 32, 33],
];

// Single row (1-based)
pprint($matrix, rowsOnly: 2);

// Sparse rows and columns: rows 1,2,3,5,6 and cols 1,2,6,9,10
pprint($matrix, rowsOnly: '1-2,3,5-6', colsOnly: '1-2,6,9-10');

// Works the same for 3D tensors (applied per 2D slice)
pprint($tensor3d, rowsOnly: '2-3', colsOnly: '1,3-4');

$matrix = [
    [1, 'x', 2.5, '4'],
    [3, 'y', 4.5, 6],
    ['n/a', 'z', 1.0, null],
];

pprint($matrix, colsSummary: true);
// Adds a final summary row with sums per visible column.
// Only int/float values are -totals---)
pprint($matrix, colsSummary: true, colsSummaryLabel: '===sum===');

// Add per-row summary as an extra trailing column
pprint($matrix, rowsSummary: true);

// Customize the row-summary header label shown in the first row
pprint($matrix, rowsSummary: true, rowsSummaryLabel: '===rows===');

// 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: '');

// Custom separator between multiple values (default is a single space " ")
pprint('A', 'B', 'C', sep: ', ', end: '');
// A, B, C

// Separator can also be provided via trailing options array
pprint('X', 'Y', sep: "\n", end: '']);
// X
// Y

$s = pprint([1, 2, 3], return: true);
// $s contains: "[1, 2, 3]\n" (no <pre> wrapping)

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]]);

class Users
{
    public function asArray(): array
    {
        return [
            ['id' => 1, 'name' => 'Alice'],
            ['id' => 2, 'name' => 'Bob'],
        ];
    }
}

$users = new Users();

pprint($users);
// Users([
//   [1, 'Alice'],
//   [2,   'Bob']
// ])

$rows = [
    ['distance' => 1.0440, 'label' => 'engaged'],
    ['distance' => 2.2361, 'label' => 'engaged'],
];

pprint($rows);
// array([
//   [1.0440, 'engaged'],
//   [2.2361, 'engaged']
// ])
bash
# Default preset (small)
php benchmarks/benchmark.php

# Estimate very large shapes without allocating memory
php benchmarks/benchmark.php --preset=100k --dry-run
php benchmarks/benchmark.php --preset=1m --dry-run

# Custom size with safety cap
php benchmarks/benchmark.php --rows=2000 --cols=2000 --max-cells=5000000