Most developers discover PHP through XAMPP, a local server, or a tutorial that immediately sets up Apache and a browser preview. So it's natural to assume PHP only lives inside a web server. But that's just one way to use it.
PHP has a fully functional Command Line Interface (CLI). You can write a .php file, run it from your terminal, and see output instantly — no browser, no server, no HTTP involved at all.
Do You Need to Install PHP Separately?
If you already have XAMPP installed, the answer is no.
XAMPP bundles PHP as part of its package. The PHP binary is already sitting on your machine — it's just not registered in your system's PATH by default, which is why your terminal doesn't recognize the php command out of the box.
You have two options:
- Add XAMPP's PHP to your system PATH — recommended, one-time setup
- Install PHP separately — only needed if you don't have XAMPP or want an independent installation
Option 1 — Use PHP From XAMPP (Windows)
Step 1 — Find Your XAMPP PHP Path
XAMPP installs PHP at:
C:\xampp\php
Confirm it by checking if php.exe exists there:
C:\xampp\php\php.exe
Step 2 — Add It to System PATH
- Press
Win + S, search "Environment Variables" - Click "Edit the system environment variables"
- Click "Environment Variables" button
- Under System variables, find and select Path, click Edit
- Click New and add:
C:\xampp\php
- Click OK on all dialogs
Step 3 — Restart Your Terminal
Close and reopen VS Code terminal or Command Prompt, then verify:
php --version
Expected output:
PHP 8.2.12 (cli) (built: Oct 24 2023)
That (cli) confirms you're running the CLI SAPI — not the web module. You're good to go.
Option 2 — Install PHP Separately (Without XAMPP)
If you want a clean, standalone PHP installation independent of XAMPP:
Windows
- Go to https://windows.php.net/download
- Download the latest VS16 x64 Non Thread Safe ZIP
- Extract it to a folder, for example:
C:\php
- Add
C:\phpto your system PATH using the same steps above - Verify:
php --version
macOS
PHP comes pre-installed on older macOS versions, but for a current version use Homebrew:
brew install php
Then verify:
php --version
Linux (Ubuntu / Debian)
sudo apt update
sudo apt install php-cli
Verify:
php --version
Note — php-cli is a minimal package. It installs only what's needed to run PHP in the terminal, without Apache or any web server component.
Running Your First PHP Script in the Terminal
Create a file anywhere on your machine — call it index.php:
<?php
$name = "Gagan";
$age = 25;
echo "Name: " . $name . PHP_EOL;
echo "Age: " . $age . PHP_EOL;
Open your terminal in that folder and run:
php index.php
Output:
Name: Gagan
Age: 25
No browser. No server. Just PHP and your terminal.
The Interactive Shell
PHP also ships with an interactive shell — write and execute PHP expressions in real time without creating a file:
php -a
Interactive shell
php > $x = 10;
php > $y = 20;
php > echo $x + $y;
30
php > echo strtoupper("hello world");
HELLO WORLD
php >
Type exit to quit. Useful for quickly testing a function, checking a value, or experimenting with syntax.
Run a One-Liner Without Any File
For something quick, you don't even need a file:
php -r "echo date('Y-m-d') . PHP_EOL;"
2026-06-05
One Important Difference — HTML Tags Don't Work Here
Since there's no browser to render markup, HTML tags will print as plain text:
<?php
echo "<h1>Hello</h1>";
Terminal output:
<h1>Hello</h1>
For CLI scripts, use PHP_EOL for line breaks instead of <br>, and avoid HTML markup entirely. The terminal only cares about plain text.
What You Can Build With PHP CLI
Once you're comfortable running PHP in the terminal, a completely different category of work opens up:
- Automation scripts — file processing, batch renaming, data cleanup
- Cron jobs — scheduled tasks that run in the background on a server
- Data migration — moving records between databases
- Custom CLI tools — interactive prompts, argument parsing, system utilities
- Running tests — PHPUnit, Pest, and all major PHP testing frameworks run entirely in the terminal
And if you use Laravel, you've already been using PHP CLI every time you typed php artisan — that entire command system is built on it.
The Bottom Line
PHP is not limited to the browser. The CLI has been part of PHP since version 4.3 and it works exactly the way you'd expect — write a file, run it, see output. If you have XAMPP, you already have everything you need, just add the path and you're set.
The browser-only workflow is a habit formed by how PHP is introduced to most developers. It was never a technical limitation.
No comments:
Post a Comment