Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Quick PHP table output for console
<?php
/**
* A quick way, with a bit of overhead, to get nice tabular output
* from a PHP CLI app.
*
* Sometimes I find myself just requiring very quick and dirty
* tabular output and find this to be the path of least resistance.
*
* This method utilises Symfony's Console's Table and BufferedOutput
* classes to generate the table and capture the built table to later
* store or output.
*
* $ composer require symfony/console
*
* https://symfony.com/doc/current/components/console/helpers/table.html
*/
declare(strict_types=1);
require 'vendor/autoload.php';
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Output\BufferedOutput;
$output = new BufferedOutput();
$table = new Table($output);
$table->setHeaders(['First Name', 'Last Name'])
->setRows([
['John', 'Smith'],
['Jane', 'Doe']
])
->setStyle('default')
->render();
echo $output->fetch();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment