Explain it simply
Setup in plain words
Setup is like getting a kitchen ready. You need the PHP program itself, a folder to keep your recipes (script files) in, and a terminal or a browser to try them out. PHP is free, so there is nothing to buy.
Today's goal is a single thing: type `php -v` in a terminal and see a version number starting with 8. Once that works, the rest of the course is just writing text files and running them.
Problem → Why → Solution
The problem: you follow a tutorial and the terminal says it has never heard of `php`
Most setup trouble looks alike. The installer has run, yet the terminal claims it does not know `php`, because the folder PHP was installed into is not on the path the terminal searches (the PATH). Or XAMPP is installed and typing `C:/xampp/htdocs/test.php` into the browser shows source code, or the terminal reports PHP 7 while the browser reports PHP 8.
All of these have the same root cause: a machine can hold more than one PHP, and you have to check for yourself which one starts how and which settings file it reads.
C:\projects> php -v
'php' is not recognized as an internal or external command,
operable program or batch file.Why fix this first
Until the `php` command works you cannot run a single lesson. Three checks, `php -v`, `php --ini` and `where php` (Windows) or `which php` (macOS and Linux), solve most setup problems.
Core concept: a PHP setup is an executable, an ini file and a way to run it
A PHP installation has three main parts: the `php` executable (used from the command line), the `php.ini` configuration file and the extensions. For the web you may add a server module or PHP-FPM. The command line alone is enough for the early lessons of this course, and for lessons that need a browser the `php -S` built-in server works without Apache.
| Option | Good for | Watch out for |
|---|---|---|
| Package manager or php.net build (Homebrew, apt, Windows zip) | Closest to production, and the `php` command works anywhere | You manage extensions and php.ini yourself |
| All-in-one bundle (XAMPP, MAMP, Laragon) | Apache, MariaDB and PHP in one installer, handy for the database lessons | The bundled PHP is often not on PATH and may differ from another install |
| Docker's official php images | An exact version that is identical on every machine | Needs some Docker knowledge, and the first start is slower |
Install PHP 8.x
Pick one of the routes above. On Windows, add the folder containing `php.exe` to PATH and open a fresh terminal.
Verify with `php -v`
The first line shows the version and the SAPI (`cli`). The version should start with 8.
Look at the configuration
`php --ini` shows the php.ini that was loaded and `php -m` lists the enabled extensions.
Run a script
Put `<?php echo "Hello", PHP_EOL;` into `hello.php` and run it with `php hello.php`.
Start a server when you need one
Run `php -S localhost:8000` inside the project folder and open `http://localhost:8000/hello.php` in the browser.
A PHP download ships two templates, `php.ini-development` and `php.ini-production`. You copy one of them to `php.ini` and use it. In development you switch `display_errors` on so mistakes appear on screen; in production error messages expose file paths and code details, so they must be written to a log and never shown to visitors.
- Install the php program
- php -v confirms version 8.x
- php.ini switches errors and extensions on
- php file.php runs your script
Each command answers one question: `php -v` which version, `php --ini` which configuration, `php -m` which extensions.
General pattern: the PHP command line
php --version # which PHP am I running?
php --ini # which php.ini was loaded?
php -m # which extensions are enabled?
php script.php [arguments] # run a script
php -S host:port [-t folder] # development web serverYou combine the `php` command with options, like asking questions. Give it a file name and it runs the script; give it `-S` and it starts the development server. `-t` chooses which folder the server exposes.
Example: a script that checks your setup
Instead of reading a huge page such as `phpinfo()`, a short script can check exactly what you care about. Save it as `check.php` and run `php check.php`. Each check prints `OK` or `MISSING`; a `MISSING` line tells you which extension to install.
<?php
declare(strict_types=1);
$checks = [
"PHP 8.0 or newer" => PHP_VERSION_ID >= 80000,
"64-bit integers" => PHP_INT_SIZE === 8,
"json extension" => extension_loaded("json"),
"mbstring extension" => extension_loaded("mbstring"),
"started from the command line" => PHP_SAPI === "cli",
];
foreach ($checks as $label => $passed) {
echo str_pad($label, 32), $passed ? "OK" : "MISSING", PHP_EOL;
}Code breakdown
- A label and a yes/no answer
"PHP 8.0 or newer" => PHP_VERSION_ID >= 80000,Each array item pairs a text label with a true/false answer. `PHP_VERSION_ID` is a number such as `80303`, so comparing it with 80000 tells you whether PHP is 8.0 or newer.
- Strict comparison
PHP_INT_SIZE === 8,`PHP_INT_SIZE` says how many bytes an integer holds. `===` is true only when both value and type match; on a 64-bit system the answer is 8.
- Asking about an extension
"mbstring extension" => extension_loaded("mbstring"),`extension_loaded()` answers with a boolean whether an extension is enabled. mbstring is needed to handle Myanmar text correctly, so it matters in this course.
- Walking through the array
foreach ($checks as $label => $passed) {`foreach` runs its body once for every item in the array. `$label` receives the key and `$passed` the value. Loops get their own lesson, number 13.
- Padding and a ternary
echo str_pad($label, 32), $passed ? "OK" : "MISSING", PHP_EOL;`str_pad($label, 32)` fills the label with spaces up to 32 characters so the results line up. `$passed ? "OK" : "MISSING"` picks `OK` when true and `MISSING` otherwise.
PHP 8.0 or newer OK
64-bit integers OK
json extension OK
mbstring extension OK
started from the command line OKCommands you will use every day
| Command | What it does |
|---|---|
| `php -v` | Shows the version and the SAPI |
| `php --ini` | Shows the php.ini file that was loaded |
| `php -m` | Lists the enabled extensions |
| `php -l file.php` | Checks syntax without running the script |
| `php -S localhost:8000` | Serves the current folder with the development server |
php.ini for development
While you are learning, the settings below are useful. `error_reporting = E_ALL` shows warnings, notices and deprecations, so even a slip like typing `$Name` for `$name` is reported immediately. Without `date.timezone`, PHP falls back to UTC, which may not match your local time. On Windows you enable an extension with a line such as `extension=mbstring`, while on Linux you usually install it with the package manager.
; development machine
display_errors = On
error_reporting = E_ALL
date.timezone = "Asia/Yangon"
extension=mbstringThe CLI re-reads php.ini every time a script starts. PHP-FPM and Apache read it only at start-up, so after editing you must reload or restart them.
Never leave a phpinfo() page public
`phpinfo()` prints the version, file paths, extensions and configuration, all of which help an attacker. Delete the file as soon as you have looked.
<?php
// Shows machine-specific details; delete this file when you are done.
phpinfo();In real projects
Setup in real projects
Teams pin the PHP version per project: in a constraint such as `"php": "^8.2"` in `composer.json`, in the Docker image tag and in the CI pipeline. Most 'it works on my machine' problems come down to a different version or a missing extension. Composer can check requirements such as `ext-mbstring`, so the gap is reported at install time.
Production servers use settings close to `php.ini-production`: `display_errors` off and `log_errors` on. Warnings that appear nicely on your own machine therefore land in a log file online instead of in front of visitors. Knowing how development and production settings differ means fewer surprises after a deploy. Writing the pinned version and the required extensions in the project README lets a new teammate verify the setup in a few minutes.
Common mistakes
Common beginner mistakes
1. Forgetting to put the PHP folder on PATH
'php is not recognized' does not mean the install failed; the terminal just cannot find `php.exe`. Add the folder containing it to PATH and open a new terminal, because PATH changes do not reach terminals that are already open. Then confirm with `where php` or `which php` which file is actually used.
2. Opening an XAMPP file by its disk path
Typing `C:/xampp/htdocs/test.php` into the address bar makes the browser read the file from disk, and PHP never runs. Start Apache and type `http://localhost/test.php`, with the file stored inside the `htdocs` folder.
3. Editing the wrong php.ini
A machine can have several php.ini files, one for the CLI and another for FPM. `php --ini` reports only the one the CLI reads, so check "Loaded Configuration File" in `phpinfo()` on a web page to learn which file the web side uses. After editing, remember to reload FPM or Apache.
Mini challenge
Mini challenge: an extension status helper
Write a typed function `extensionStatus` that takes an extension name and returns a string such as `json: OK` or `json: MISSING`. Then print whether PHP is version 8 or newer.
- Give the function one `string` parameter and a `string` return type
- Use `extension_loaded()` and the ternary `? :`
- Print the status for both `json` and the non-existent `definitely_not_an_extension`
- Check `PHP_MAJOR_VERSION >= 8` and print `yes` or `no`
<?php
declare(strict_types=1);
// TODO: add a string parameter type and a string return type
function extensionStatus($name)
{
// TODO: return "<name>: OK" or "<name>: MISSING" using extension_loaded()
}
// TODO: print the status of "json" and of "definitely_not_an_extension"
// TODO: print whether PHP_MAJOR_VERSION is 8 or newer ("yes" / "no")Expected: Prints `json: OK`, `definitely_not_an_extension: MISSING` and `Running PHP 8 or newer: yes`, one per line.
Show hint
Wrap the ternary in parentheses: `$name . ": " . (extension_loaded($name) ? "OK" : "MISSING")`. Without them the order of `.` and `? :` can give a surprising result.
Show solution
<?php
declare(strict_types=1);
function extensionStatus(string $name): string
{
return $name . ": " . (extension_loaded($name) ? "OK" : "MISSING");
}
echo extensionStatus("json"), PHP_EOL;
echo extensionStatus("definitely_not_an_extension"), PHP_EOL;
echo "Running PHP 8 or newer: ", PHP_MAJOR_VERSION >= 8 ? "yes" : "no", PHP_EOL;Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- A PHP install is the `php` executable, php.ini and extensions, and `php -v` is the first check
- `php file.php` runs a script and `php -S localhost:8000` starts the development server
- `php --ini` and `php -m` tell you which configuration and extensions are in use
- Show errors in development but log them in production, and never leave `phpinfo()` public
Migrating to PHP 8.5 — PHP Documentation Group