Convert PHP to HTML on the Command Line

Generating static HTML from PHP scripts without a web server can be useful for pre-rendering pages, debugging, or using PHP as a templating engine. This guide walks through how to convert PHP to HTML using the command line.

The objective of this article is to show how to output an HTML web page from an input PHP script without using a web browser. Instead the output HTML page is generated on the command line in a terminal window.

Table of Contents

Why Convert PHP to Static HTML?

There are several reasons why you might want to generate static HTML from PHP:

  • Reduce Server Load – Pre-rendering pages reduces the need for PHP execution on every request.
  • Improve Security – By serving only static files, you eliminate the risk of PHP vulnerabilities.
  • Use PHP as a Static Site Generator – PHP can generate dynamic content once, then store it as static files for deployment.
  • Debugging and Testing – Running PHP in a terminal allows you to quickly check output without setting up a web server.

How to Convert PHP to HTML in the Command Line

To generate HTML output from a PHP file, follow these steps:

1. Install php-cgi

By default, running php from the command line strips out HTML tags. To preserve them, install php-cgi.

For Debian-based systems (Ubuntu, Linux Mint, etc.):

sudo apt install php-cgi

For Red Hat-based systems (Fedora, CentOS):

yum install php-cli

If using Windows, ensure PHP is installed and available in your system PATH.

2. Convert PHP to HTML

To generate an HTML file from a PHP script, use the following command with your own input and output file names:

php-cgi -q input.php > output.html

The -q flag prevents HTTP headers from being added to the output.

Example: Generating a PHP Info Page

To see PHP configuration details in an HTML format, create a PHP script called phpinfo.php with the following contents:

<?php phpinfo(); ?>

Then, run this command:

php-cgi -q phpinfo.php > phpinfo.html

This creates phpinfo.html, which you can open in a browser to view the PHP configuration output as a styled HTML page. The following image shows the generated HTML page open in a web browser and displaying the contents correctly.

Screenshot of the PHP info page generated using php-cgi, showing PHP configuration details in an HTML format.
PHP info page generated using php-cgi, displaying PHP configuration details in a formatted HTML output.

Alternative Methods to Convert PHP to HTML

1. Using php Instead of php-cgi

Another approach is to capture HTML output using the standard php command:

php input.php > output.html

Although this method works in many cases, it might not preserve all expected formatting due to execution differences. Testing this method with the phpinfo.php example script gave the following unformatted output, which is clearly not the desired result.

Unformatted PHP info output generated using the php command, showing raw text instead of structured HTML.
Unformatted PHP Output Using php Command

2. Using curl or wget

If you have a local development server running, you can fetch a PHP-generated HTML page with:

curl http://localhost/page.php > page.html

Or using wget:

wget -q -O page.html http://localhost/page.php

Understanding php-cgi Options

The -q switch used in the example above stops HTTP header output from being placed at the top of the output HTML file which looks as follows if -q is not used.

Content-type: text/html; charset=UTF-8

The php-cgi command offers various useful flags:

  • -q – Suppresses HTTP headers (useful for clean output)
  • -f <file> – Runs a PHP script
  • -i – Displays PHP info
  • -m – Shows loaded modules
  • -l – Performs syntax check only
  • -w – Outputs script without comments and whitespace

Conclusion

Knowing how to convert PHP to HTML on the command line is a valuable technique. It improves performance, enhances security, and simplifies debugging. Whether you’re pre-rendering pages, reducing server load, or using PHP as a static site generator, these methods ensure a straightforward way to extract HTML output from PHP scripts efficiently.

Leave a Comment