start here

Installing and Running PHP

Installing PHP is not finished when a package appears on the machine. A project needs the correct PHP version, the correct active binary, the required extensions, a suitable php.ini, and a way to prove all of that from the terminal.

This lesson is about verification. Installation steps differ by operating system, but the checks that prove PHP is ready are the same professional habit everywhere.

Common Ways PHP Gets Installed

PHP can come from several places:

  • an operating-system package manager
  • Homebrew on macOS
  • a Windows installer or zip distribution
  • a development stack such as XAMPP, Laragon, MAMP, or Herd
  • Docker or another container image
  • a version manager
  • a compiled-from-source build

Those options can install PHP into different directories. A machine can also have more than one PHP version installed at the same time.

That is why the first check is always the active binary.

Find The Active PHP Binary

On macOS and Linux:

which php
php -v

On Windows PowerShell:

where php
php -v

Example output:

/usr/bin/php
PHP 8.5.6 (cli)

The path tells you which executable your shell found. The version tells you what that executable actually runs.

If a project expects PHP 8.3 and php -v shows PHP 8.1, the problem is not your application code. It is the runtime.

Check Configuration Files

PHP loads configuration from php.ini and optional additional .ini files.

php --ini

Typical output includes:

Loaded Configuration File: /etc/php.ini
Scan for additional .ini files in: /etc/php.d

This matters because CLI PHP and web PHP may not load the same configuration. If you install an extension for one runtime but not the other, the terminal and browser can behave differently.

Check Extensions

Extensions provide features that many projects rely on.

php -m
php -m | grep pdo
php -m | grep mbstring

On Windows PowerShell:

php -m
php -m | Select-String pdo
php -m | Select-String mbstring

From PHP code:

PHP example
<?php

declare(strict_types=1);

$requiredExtensions = ['mbstring', 'json', 'pdo'];

foreach ($requiredExtensions as $extension) {
    echo $extension . ': ';
    echo extension_loaded($extension) ? 'loaded' : 'missing';
    echo PHP_EOL;
}

// Prints:
// mbstring: loaded
// json: loaded
// pdo: loaded

Missing extensions often produce confusing framework or Composer errors. Checking them directly is faster than guessing.

Use Composer Platform Checks

Composer projects can declare required PHP versions and extensions in composer.json.

{
  "require": {
    "php": "^8.3",
    "ext-mbstring": "*",
    "ext-pdo": "*"
  }
}

After installing PHP, run:

composer check-platform-reqs

This checks the installed PHP runtime against the project's declared requirements. It is a strong signal that your machine matches the project closely enough to start working.

Run A First Script

Create hello.php:

PHP example
<?php

declare(strict_types=1);

echo 'Hello from PHP ' . PHP_VERSION . PHP_EOL;

// Prints:
// Hello from PHP 8.5.6

Run it:

php hello.php

Then syntax-check it:

php -l hello.php

This proves PHP can execute a script from your shell.

Run A First Local Web Request

Create public/index.php:

PHP example
<?php

declare(strict_types=1);

header('Content-Type: text/plain');

echo 'Hello over HTTP' . PHP_EOL;
echo 'SAPI: ' . PHP_SAPI . PHP_EOL;

// Browser output:
// Hello over HTTP
// SAPI: cli-server

Start the built-in server:

php -S 127.0.0.1:8000 -t public

Open:

http://127.0.0.1:8000/

This proves PHP can handle a local HTTP request. It does not prove production PHP-FPM or a real web server is configured.

What To Record For A Project

For a real project, make setup reproducible by recording:

  • required PHP version
  • required extensions
  • how PHP is installed locally
  • how to find the active binary
  • how to check php.ini
  • how to run the local server or container
  • how to run Composer's platform check

This belongs in project setup documentation because future developers will hit the same environment questions.

What You Should Be Able To Do

After this lesson, you should be able to verify an installed PHP runtime, identify the active binary, inspect loaded configuration, check required extensions, run a PHP script, and start a local HTTP server.

For junior PHP work, this matters because many setup bugs are environment bugs. A developer who can prove the runtime is correct can unblock themselves and help teammates faster.

Practice

Practice: Verify A PHP Installation

Create a short setup note that proves PHP is installed and runnable.

Task

Include commands that show:

  • active PHP version
  • active PHP binary path
  • loaded php.ini files
  • loaded extensions
  • a syntax check for a small hello.php file

Also create a small hello.php script that prints the PHP version.

Use strict types. Keep example output inside code blocks.

Afterward, add a short note explaining why checking the active binary matters when multiple PHP versions exist.

Show solution

This setup note proves the CLI PHP runtime is installed and usable.

which php
php -v
php --ini
php -m
php -l hello.php
php hello.php

On Windows PowerShell, use where php instead of which php.

PHP example
<?php

declare(strict_types=1);

echo 'Hello from PHP ' . PHP_VERSION . PHP_EOL;

// Prints:
// Hello from PHP 8.5.6

Checking the active binary matters because a machine can have several PHP versions installed. Your terminal may run a different PHP executable from the one your editor, web server, container, or project expects.

Task: Find Active Binary

Create a short diagnostic checklist for proving which PHP binary your shell is using.

Requirements

Include commands for:

  • macOS/Linux
  • Windows PowerShell
  • PHP version
  • loaded configuration path

Then write a short note describing what you would compare if Composer says the PHP version is wrong.

Show solution
which php
php -v
php --ini

On Windows PowerShell:

where php
php -v
php --ini

Example output:

/usr/bin/php
PHP 8.5.6 (cli)
Loaded Configuration File: /etc/php.ini

If Composer says the PHP version is wrong, compare the php path from your shell with the PHP path Composer is using. Also check whether the project is running inside a container, IDE terminal, version manager, or different shell where PATH may be different.

Task: Check Required Extension

Create a small extension checker for a project setup guide.

Requirements

Build a PHP script that checks whether these extensions are loaded:

  • json
  • mbstring
  • pdo

The script should print one line per extension and exit with 1 if any are missing.

Use strict types. Keep expected output inside the PHP code block as comments.

Afterward, add a short note explaining how this relates to Composer platform requirements.

Show solution

This script checks required extensions and returns a useful exit code for automation.

PHP example
<?php

declare(strict_types=1);

$requiredExtensions = ['json', 'mbstring', 'pdo'];
$missing = [];

foreach ($requiredExtensions as $extension) {
    $loaded = extension_loaded($extension);
    echo $extension . ': ' . ($loaded ? 'loaded' : 'missing') . PHP_EOL;

    if (!$loaded) {
        $missing[] = $extension;
    }
}

if ($missing !== []) {
    fwrite(STDERR, 'Missing extensions: ' . implode(', ', $missing) . PHP_EOL);
    exit(1);
}

exit(0);

// Prints:
// json: loaded
// mbstring: loaded
// pdo: loaded

Composer platform requirements declare the PHP version and extensions a project needs. A script like this is a small local check; composer check-platform-reqs is the project-level check against composer.json.