Skip to content
PHP Misconceptions

Chapter 07

Is PHP Only for WordPress?

Run a PHP inventory report as a command and as a JSON endpoint, then see where frameworks and CMSs fit.

WordPress is a PHP application with its own plugin ecosystem. PHP can also run a CLI tool, return JSON from an API or process jobs as a background worker. You can choose plain PHP for a small program, individual Symfony components for selected features or a framework like Laravel for routing, authentication and testing conventions. WordPress plugin handbook · PHP CLI · Symfony components · Laravel routing

With PHP 8.6 or later, run the inventory report from the repository root.

Terminal window
php code/php-misconceptions/inventory.php
{"items":[{"sku":"PHP-101","stock":12},{"sku":"PHP-102","stock":0}]}

Now start PHP’s development server in the first terminal. Keep it local.

Terminal window
php -S 127.0.0.1:8027 -t code/php-misconceptions

Inspect the response from a second terminal. Then stop the server with Ctrl+C.

Terminal window
curl -i http://127.0.0.1:8027/inventory.php

You should see HTTP 200, a JSON content type and the same inventory data. The script sends a header in its web environment and prints the JSON in both environments. The built-in server is for development. PHP built-in web server

Add a third product and check that both calls return the same three items. If you later add accounts, persistent inventory and several routes, the same report can become part of a Laravel application with migrations and HTTP tests. Laravel responses

Is PHP Inherently Insecure?

Report a problem with this example