This is a card in Dave's Virtual Box of Cards.

PHP as a text generation tool

Page created: 2023-01-15

There are a billion template languages out there. But the one I know by heart because I’ve been using it to make websites for some two decades and the one that is always installed is PHP.

For really tiny things, bash works great. But beyond the tiniest line of text, PHP rules!

Generating a chunk of text or markup is so easy in PHP because it’s purpose-built to do it. You don’t need a web server or anything. Just run the interpreter from the command line and pipe the output wherever you want.

I do it all the time. Here’s a very typical example from a browser game I’m working on:


$suit = "Swords";
$fname = "swords";
$names = [

for($i=1; $i<=14; $i++): ?>
    <div class="card">
        <img src="<?=$fname?><?=$i?>.jpg" alt="">
        <div class="desc">
            <h2><?=$names[$i]?> of <?=$suit?></h2>
            <p><strong>Traditional Meaning:</strong>
<?php endfor ?>

It’s the dumbest code you’ll ever see, but it was so easy to write and then PHP did the work and now that part of the game is done forever.