start here

Beginner Runtime Path

PHP is not only a language. It is also a program installed on your machine, a set of configuration files, optional extensions, and several ways to run code.

Before you can debug real PHP projects, you need to know which PHP binary is running, which configuration it loaded, which extensions are enabled, and whether the code is running from the command line or through a web server.

The PHP Binary And PATH

When you type php, your shell looks through the directories listed in PATH and runs the first matching executable.

which php
php -v

Typical output might look like this:

/usr/bin/php
PHP 8.5.6 (cli)

The exact path and version depend on your machine. The important habit is checking them instead of assuming the project is using the PHP version you expected.

On Windows, the equivalent command is:

where php
php -v

If php -v fails, PHP is either not installed or not available on your PATH.

CLI PHP

CLI means command-line interface. It is the PHP runtime used when you run scripts, Composer commands, test suites, static analysis, queue workers, and framework console commands.

php script.php
php -r "echo PHP_VERSION . PHP_EOL;"
php -l public/index.php

php -l checks a file for syntax errors without running the application. It is a useful quick check when you are editing PHP files.

Here is a tiny CLI script:

PHP example
<?php

declare(strict_types=1);

echo 'Running on PHP ' . PHP_VERSION . PHP_EOL;

// Prints:
// Running on PHP 8.5.6

Your exact version number may be different.

Local Web Server

Running PHP from a browser is different from running it from the CLI. A web request has HTTP headers, a URL, a document root, query strings, cookies, and server variables.

For local learning, PHP has a built-in development server:

php -S 127.0.0.1:8000 -t public

That command says:

  • listen on 127.0.0.1:8000
  • serve files from the public directory
  • route PHP requests through the built-in server

A minimal public/index.php can show the difference between CLI and web execution:

PHP example
<?php

declare(strict_types=1);

echo 'SAPI: ' . PHP_SAPI . PHP_EOL;
echo 'Method: ' . ($_SERVER['REQUEST_METHOD'] ?? 'not a web request') . PHP_EOL;

From the browser, PHP_SAPI will normally say cli-server when using the built-in server. From php public/index.php, it will say cli.

The built-in server is for development only. Production normally uses PHP-FPM behind Nginx, Apache with PHP-FPM, Apache's PHP module, or a specialist runtime.

Installation Is Only The First Step

Installing PHP gives you a runtime, but a project may still require:

  • a specific PHP version
  • Composer
  • extensions such as pdo_mysql, mbstring, intl, curl, zip, or gd
  • a suitable php.ini
  • environment variables
  • a local database, cache, mail tool, or queue worker

The first professional question is not "is PHP installed?" It is "is this the PHP runtime this project expects?"

Useful checks:

php -v
php -m
php --ini
composer check-platform-reqs

php -m lists loaded extensions. php --ini shows which configuration files PHP loaded. composer check-platform-reqs checks whether your installed PHP version and extensions match the project's Composer requirements.

php.ini

php.ini is the main PHP configuration file. It controls settings such as memory limits, upload limits, error reporting, timezones, and extension loading.

php --ini
php -i | grep "Loaded Configuration File"

On Windows PowerShell:

php --ini
php -i | Select-String "Loaded Configuration File"

Different SAPIs can load different configuration. CLI PHP and PHP-FPM may not use the same php.ini, so a setting that works in a terminal may not be active for a web request.

You can inspect important settings from PHP:

PHP example
<?php

declare(strict_types=1);

echo 'memory_limit=' . ini_get('memory_limit') . PHP_EOL;
echo 'upload_max_filesize=' . ini_get('upload_max_filesize') . PHP_EOL;
echo 'date.timezone=' . ini_get('date.timezone') . PHP_EOL;

When a project behaves differently between the browser and terminal, compare the loaded configuration for both runtimes.

Extensions

Extensions add features to PHP. Some are built in, some are enabled by packages, and some are optional.

Common examples:

  • pdo_mysql for MySQL database access
  • mbstring for multibyte string handling
  • intl for locale-aware formatting
  • curl for HTTP requests
  • zip for archive handling
  • gd or imagick for image work

Check loaded extensions with:

php -m
php -m | grep mbstring

From PHP code:

PHP example
<?php

declare(strict_types=1);

echo extension_loaded('mbstring') ? 'mbstring loaded' : 'mbstring missing';
echo PHP_EOL;

Missing extensions are a common cause of "works on my machine" bugs. Composer can declare required extensions, but the machine still needs to have them installed and enabled.

Environment Variables

Environment variables are values provided outside the code. Projects use them for configuration that changes between local, staging, and production environments.

Examples include database hosts, API keys, feature flags, and app modes.

APP_ENV=local php -r "echo getenv('APP_ENV') . PHP_EOL;"

Inside PHP:

PHP example
<?php

declare(strict_types=1);

$environment = getenv('APP_ENV') ?: 'production';

echo 'Environment: ' . $environment . PHP_EOL;

In real projects, do not hard-code secrets in PHP files. Load them through environment configuration, secret managers, or the framework's configuration system.

A Beginner Runtime Checklist

When a PHP project does not run, check the runtime before changing application code:

  1. Which php binary is running?
  2. Which PHP version is it?
  3. Is this CLI, built-in server, PHP-FPM, or another SAPI?
  4. Which php.ini files are loaded?
  5. Are the required extensions loaded?
  6. Are required environment variables available?
  7. Does Composer agree that the platform requirements are satisfied?

This checklist prevents a lot of wasted debugging. Many beginner PHP problems are runtime mismatches, not application bugs.

What You Should Be Able To Do

After this lesson, you should be able to find your PHP binary, check the PHP version, run a CLI script, start the built-in local server, locate loaded php.ini files, list extensions, and read an environment variable.

For junior PHP work, this matters because setup and debugging are part of the job. A developer who can prove what runtime is actually executing the code is much more useful than one who guesses.

Practice

Practice: Inspect Your PHP Runtime

Create a small runtime report that proves which PHP runtime is executing your code.

Task

Build a PHP script that prints:

  • PHP_VERSION
  • PHP_SAPI
  • the loaded php.ini path from php_ini_loaded_file()
  • whether mbstring is loaded
  • the value of APP_ENV, falling back to not set

Run it once from the CLI. If you have a public directory, run it again through the built-in server and compare PHP_SAPI.

Use strict types. Keep example output inside the PHP code block as comments or printed lines.

Afterward, add a short note explaining why runtime checks should happen before changing application code.

Show solution

This solution prints the runtime details that usually explain local setup problems.

PHP example
<?php

declare(strict_types=1);

$loadedIni = php_ini_loaded_file();
$appEnvironment = getenv('APP_ENV');

echo 'PHP version: ' . PHP_VERSION . PHP_EOL;
echo 'SAPI: ' . PHP_SAPI . PHP_EOL;
echo 'Loaded php.ini: ' . ($loadedIni === false ? 'none' : $loadedIni) . PHP_EOL;
echo 'mbstring: ' . (extension_loaded('mbstring') ? 'loaded' : 'missing') . PHP_EOL;
echo 'APP_ENV: ' . ($appEnvironment === false ? 'not set' : $appEnvironment) . PHP_EOL;

// Example CLI output:
// PHP version: 8.5.6
// SAPI: cli
// Loaded php.ini: /etc/php.ini
// mbstring: loaded
// APP_ENV: not set

Run it from the command line:

php runtime-report.php

Run it with an environment variable:

APP_ENV=local php runtime-report.php

If the script is available inside public/runtime-report.php, you can compare the web SAPI:

php -S 127.0.0.1:8000 -t public

Runtime checks should happen before changing application code because the bug may be the wrong PHP version, missing extension, different php.ini, or missing environment variable. Fixing application logic will not solve a runtime mismatch.