Skip to content

Instantly share code, notes, and snippets.

@othyn
Created January 7, 2020 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save othyn/a933d8084066cc7fa2cda4d034f032af to your computer and use it in GitHub Desktop.
Save othyn/a933d8084066cc7fa2cda4d034f032af to your computer and use it in GitHub Desktop.
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