PHPackages                             spydr97/php-cli-table - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [CLI &amp; Console](/categories/cli)
4. /
5. spydr97/php-cli-table

ActiveLibrary[CLI &amp; Console](/categories/cli)

spydr97/php-cli-table
=====================

A simple library to create clean table outputs in PHP CLI scripts

1.0.1(2y ago)025↓100%MITPHPPHP ^8.1

Since Dec 26Pushed 2y ago1 watchersCompare

[ Source](https://github.com/spydr97/php-cli-table)[ Packagist](https://packagist.org/packages/spydr97/php-cli-table)[ RSS](/packages/spydr97-php-cli-table/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (1)Versions (3)Used By (0)

PHP CLI Table
=============

[](#php-cli-table)

---

[![example branch parameter](https://github.com/spydr97/php-cli-table/actions/workflows/phpcs.yml/badge.svg?branch=main)](https://github.com/spydr97/php-cli-table/actions/workflows/phpcs.yml/badge.svg?branch=main)

[![example.png](example.png)](example.png)

This project's functionality was inspired by [clitable](https://github.com/jc21/clitable)and was created because I wanted some additional functionality. The code has undergone a full re-write with a bunch of changes to behaviour and functionality

Features
--------

[](#features)

- Print table from data array without defining additional config
- Define custom table columns
- Create formatters for fields using closures
- Colorize headers, columns, rows and individual cells conditionally using closures
- *\[Not well tested\]* Colorize text within a table cell

Requirements
------------

[](#requirements)

- PHP 8.1 or higher
- [Composer set up in the desired project](https://getcomposer.org/doc/01-basic-usage.md)
- A CLI environment to run PHP scripts

Installation
------------

[](#installation)

```
composer require spydr97/php-cli-table
```

Usage
-----

[](#usage)

### Basic Usage

[](#basic-usage)

For detailed examples and more complex usage please see the scripts in the example folder The bare minimum required to render a table is the following.

```
use Spydr97\PhpCliTable\CliTableBuilder;

$data = [
    [
        'id' => 1,
        'name' => 'Hello',
    ],
    [
        'id' => 2,
        'text' => 'World',
    ],
];

(new CliTableBuilder())
    ->setData($data)
    ->build();
```

### Builder Methods

[](#builder-methods)

In the absence of proper documentation, (I haven't decided how I want to do that just yet) here are the methods exposed by the `CliTableBuilder` that can be used to manipulate the resultant table.

- `setData(array $data)` - sets the data array to populate the table
- `setFields(array $fields)` - sets the fields array to define table columns
- `setBorderColor(TextColorEnum $color)` - sets the color of the table border
- `setHeaderColor(TextColorEnum $color)` - sets the default color of each table heading (superseded by the `FIELD_HEADER_COLOR` property in a field definition)
- `setCellColor(TextColorEnum $color)` - sets the default color of each table cell (superseded by the `FIELD_COLUMN_COLOR` property in a field definition or `DATA_COLOR` property in a data item)
- `setEmptyCellPlaceholder(string $empty_cell_placeholder)` - sets the text to show when the particular data item value does not exist
- `setShowHeader(bool $show_header)` - sets whether to show or hide table headings
- `build()` - validates data and prints the table to the console

### Colors

[](#colors)

Colors are defined in the `\Spydr97\PhpCliTable\TextColorEnum` class. The following colors are available

- `TextColorEnum::BLACK`
- `TextColorEnum::WHITE`
- `TextColorEnum::DARK_RED`
- `TextColorEnum::RED`
- `TextColorEnum::DARK_GREEN`
- `TextColorEnum::GREEN`
- `TextColorEnum::DARK_YELLOW`
- `TextColorEnum::YELLOW`
- `TextColorEnum::DARK_BLUE`
- `TextColorEnum::BLUE`
- `TextColorEnum::DARK_CYAN`
- `TextColorEnum::CYAN`
- `TextColorEnum::LIGHT_GREY`
- `TextColorEnum::DARK_GREY`
- `TextColorEnum::RESET`

### Data

[](#data)

In addition to data fields, each row can have a `_color` (`DataConstants::DATA_COLOR`) property which defines the color of the particular row. This can either be of type `TextColorEnum` or `Closure`. If a `TextColorEnum` is used then the color is applied on the whole row. If a `Closure` is used the color can either be applied to the whole row or to specific fields.

A `DataConstants::DATA_COLOR` closure is provided two params, `$datum` and `$field` which correspond to the current data row item and column field definition respectively. The return type of this closure must be either a `TextColorEnum` or `null`.

e.g. This closure sets the color of every column for the particular row to `BLUE` except the `text` column which is left as the configured default (i.e. not overridden).

```
use \Spydr97\PhpCliTable\Constants\DataConstants;
...
$data = [
   ...
   DataConstants::DATA_COLOR => function (array $datum, array $field): ?TextColorEnum {
            if ($field[FieldConstants::FIELD_KEY] == 'text') {
                return null;
            }
            return TextColorEnum::BLUE;
        }
]
```

### Fields

[](#fields)

Fields can have the following properties defined.

Property NameTypeClosure Return TypeRequiredFieldConstantKey`String`---`true``FIELD_KEY`Name`String`---`false``FIELD_NAME`Column Color`TextColorEnum`
`Closure``TextColorEnum`
`null``false``FIELD_COLUMN_COLOR`Header Color`TextColorEnum`
`Closure``TextColorEnum`
`null``false``FIELD_HEADER_COLOR`Formatter`Closure``String``false``FIELD_FORMATTER`The closure definitions for the above Closures are as follows:

```
use Spydr97\PhpCliTable\Constants\FieldConstants;
...
$fields = [
    ...
    [
        ...
        FieldConstants::FIELD_HEADER_COLOR => function (array $field): ?TextColorEnum {
            return TextColorEnum::DARK_RED;
        },

        FieldConstants::FIELD_COLUMN_COLOR => function (array $datum, array $field): ?TextColorEnum {
            return TextColorEnum::DARK_RED;
        },

        FieldConstants::FIELD_FORMATTER => function (array $datum, array $field): string {
            return strtoupper($datum[$field[FieldConstants::FIELD_KEY]]);
        },
        ...
    ]
]
```

Please note that the `FieldConstants::FIELD_HEADER_COLOR` closure only has a single parameter `$field`.

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

2

Last Release

864d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/29d3ef7424e6c19c310dd0bbaf428d4d04c0cc147f03c7915850361526982b17?d=identicon)[spydr97](/maintainers/spydr97)

---

Top Contributors

[![spydr97](https://avatars.githubusercontent.com/u/22036313?v=4)](https://github.com/spydr97 "spydr97 (16 commits)")

---

Tags

cliphp-libraryphp8table

### Embed Badge

![Health badge](/badges/spydr97-php-cli-table/health.svg)

```
[![Health](https://phpackages.com/badges/spydr97-php-cli-table/health.svg)](https://phpackages.com/packages/spydr97-php-cli-table)
```

###  Alternatives

[wp-cli/wp-cli

WP-CLI framework

5.0k17.2M318](/packages/wp-cli-wp-cli)[consolidation/annotated-command

Initialize Symfony Console commands from annotated command class methods.

22569.8M18](/packages/consolidation-annotated-command)[chi-teck/drupal-code-generator

Drupal code generator

26947.8M5](/packages/chi-teck-drupal-code-generator)[seld/cli-prompt

Allows you to prompt for user input on the command line, and optionally hide the characters they type

24725.8M17](/packages/seld-cli-prompt)[illuminate/console

The Illuminate Console package.

12944.1M5.1k](/packages/illuminate-console)[php-tui/php-tui

Comprehensive TUI library heavily influenced by Ratatui

589747.0k6](/packages/php-tui-php-tui)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
