PHP: Setting Up Your Development Environment
To write PHP, you need just two things: a server environment that can run PHP, and a code editor. This lesson helps you set up both—and then write your very first line of PHP code.
1. Choosing a PHP Development Environment
Running PHP requires a web server + PHP engine + database. You can install Apache/Nginx, PHP, and MySQL separately, but it's much simpler to use an all-in-one package that works out of the box.
| Package | Platform | Notes |
|---|---|---|
| XAMPP | Windows / Mac / Linux | Most popular; Apache + MariaDB(MySQL) + PHP + Perl |
| Laragon | Windows | Lightweight and fast, auto virtual hosts — recommended for Windows users |
| MAMP | Mac / Windows | Top choice for Mac users |
| Laravel Herd | Mac / Windows | Official Laravel tool, but framework-focused |
This tutorial uses XAMPP (cross-platform, full-featured, easy to configure). If you're on Windows, Laragon is also an excellent choice.
2. Installing XAMPP
(1) Step 1: Download
Go to https://www.apachefriends.org/ and click the Download button for your operating system.
(2) Step 2: Install
Double-click the downloaded installer and follow the prompts. Keep these points in mind:
- Installation path: Use the default
C:\xampp\(Windows) or/Applications/XAMPP/(Mac) - Component selection: At minimum, check Apache, MySQL, PHP, and phpMyAdmin
- The firewall may prompt you during installation — click "Allow access"
(3) Step 3: Start
Open the XAMPP Control Panel and click the Start buttons next to Apache and MySQL:
XAMPP Control Panel
┌──────────────────────────────────────────┐
│ Apache [Start] → Ports 80, 443 │
│ MySQL [Start] → Port 3306 │
│ FileZilla [Start] (not needed) │
│ Mercury [Start] (not needed) │
│ Tomcat [Start] (not needed) │
└──────────────────────────────────────────┘
When you see "Running" with a green background next to Apache and MySQL, everything is up.
(4) Step 4: Verify
Open your browser and visit http://localhost/. If you see the XAMPP welcome page, everything is working.
Then visit http://localhost/phpmyadmin/. If you see the phpMyAdmin interface, MySQL is running too.
3. Installing VS Code
You only need a text editor to write PHP code. We recommend VS Code (free, lightweight, rich extension ecosystem).
After installing, add these two recommended extensions:
| Extension | Purpose |
|---|---|
| PHP Intelephense | Code suggestions, autocomplete, error checking |
| PHP Server | One-click PHP built-in server (optional; you won't need this with XAMPP) |
Click the Extensions icon in the VS Code sidebar (or Ctrl+Shift+X), search for the extension name, and click Install.
4. Your First PHP Program
(1) Find XAMPP's Web Root Directory
XAMPP serves files from the htdocs directory:
| System | Path |
|---|---|
| Windows | C:\xampp\htdocs\ |
| Mac | /Applications/XAMPP/htdocs/ |
| Linux | /opt/lampp/htdocs/ |
Inside htdocs, create a new folder called myphp:
htdocs/
└── myphp/
└── index.php ← We'll create this file
(2) Create index.php
Open the htdocs\myphp\ folder in VS Code (File → Open Folder), then create a new file called index.php and enter the following code:
▶ Example: Hello World
<?php
echo "Hello, PHP!";
echo "<br>";
echo "Today is " . date("F j, Y");
echo "<br>";
// Display PHP version and configuration info
phpinfo();
?>
Output:
Output displayed
Save the file, then visit http://localhost/myphp/ in your browser. You should see:
- "Hello, PHP!"
- Today's date
- A purple PHP configuration info page (generated by
phpinfo())
Congratulations! You've successfully run your first PHP program.
echo is PHP's most commonly used output statement — it sends content to the HTML page. The date() function returns the current time. The . (dot) is PHP's string concatenation operator. We'll cover all of these in detail in later lessons.
5. PHP File Rules
| Rule | Explanation |
|---|---|
| File extension | Must be .php (not .html) |
| PHP tags | PHP code goes between <?php and ?> |
| File location | Must be inside the web server's document root (e.g., htdocs) |
| How to access | Visit via http://localhost/... — you can't double-click the file to open it |
| Pure PHP files | If a file contains only PHP code, you can omit the closing ?> |
▶ Example: Pure PHP File (Recommended Without ?>)
<?php
$name = "John";
$age = 18;
echo "My name is {$name} and I'm {$age} years old.";
?> in pure PHP files is a PHP community best practice. Why? Because any whitespace or blank lines after ?> can be accidentally output, which may cause HTTP header send failures. This is a small detail, but adopting good habits early matters.
6. How PHP Runs — Summary
┌──────────┐ Request ┌──────────┐ Execute PHP ┌──────────┐
│ Browser │ ──────────→ │ Apache │ ──────────→ │ PHP Engine│
│ │ ←─────────── │ (Web Srv) │ ←────────── │ │
└──────────┘ HTML Resp. └──────────┘ Generate HTML└──────────┘
↑ ↓
└─────────────── http://localhost/ ─────────────────────┘
PHP must be accessed through a web server (http://localhost/...). You can't double-click a .php file the way you can with .html files. Double-clicking a .php file will show you the raw source code, not the executed result.
▶ Example: Checking Your PHP Version
<?php
echo "PHP Version: " . PHP_VERSION . "<br>";
echo "Running on: " . PHP_OS . "<br>";
echo "Default charset: " . ini_get("default_charset") . "<br>";
if (version_compare(PHP_VERSION, "8.2.0", ">=")) {
echo "✅ PHP 8.2+ — all features in this tutorial are available";
} else {
echo "⚠️ Please upgrade to PHP 8.2+ for full compatibility";
}
Output:
Output displayed
❓ FAQ
LoadModule php_module configuration line.📖 Summary
- XAMPP bundles Apache + PHP + MySQL — install once, ready to go
- XAMPP's web root is
htdocs/— PHP files must go there to be accessible via the browser - PHP code goes inside
<?php ?>tags; files must have the.phpextension - Access PHP pages via
http://localhost/— never double-click to open echooutputs content to the page;.concatenates strings- For pure PHP files, omit the closing
?>tag
📝 Exercises
- Install XAMPP and VS Code, start Apache, and visit
http://localhost/. Take a screenshot of the XAMPP welcome page. - Inside
htdocs/myphp/, createabout.phpand useechoto output your name, city, and the current time. - Visit
http://localhost/myphp/about.phpand confirm it works. Take a screenshot of the result.