tables-php

<?php

function represent($i) {
    $symbs = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    if($i < 36)
        return $symbs[$i];
    else
        return '[' + $i + ']';
};

function show_int_at_base($basen, $val) {
    $result = '';
    while(true) {
        $quot = floor($val / $basen);
        $rem = $val % $basen;
        $val = $quot;
        $result = represent($rem) . $result;
        if($val == 0)
            break;
    }
    return $result;
}

function show_base($basen, $val) {
    $result = show_int_at_base($basen, $val);
    $pad = 2;
    if(strlen($result) < $pad) {
        return str_repeat(' ',$pad-strlen($result)) . $result;
    } else {
        return $result;
    }
}

function row($oper, $basen, $i) {
    $result = '|';
    for($start=0; $start<$basen; ++$start)
        $result .= ' ' . show_base($basen, $oper($start, $i)) . ' |';
    return $result;
}

function table($oper, $basen){
    $result = '';
    for($start=0; $start<$basen; ++$start)
        $result .= '| ' . show_base($basen, $start) . ' ' . row($oper, $basen, $start) . "\n";
    return $result."\n";
}

function md_table($oper, $basen){
    return '|    ' . row(function($x,$y){return $x*$y;}, $basen, 1) . "\n|".
        str_repeat(':--:|', $basen+1)."\n".table($oper, $basen);
}

$BASE = +$argv[1];

echo md_table(function($x,$y){return $x+$y;}, $BASE);
echo md_table(function($x,$y){return $x*$y;}, $BASE);