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.

💡 Tip: Choose a version with PHP 8.2 or 8.3. All code in this tutorial requires PHP 8.2+.

(2) Step 2: Install

Double-click the downloaded installer and follow the prompts. Keep these points in mind:

(3) Step 3: Start

Open the XAMPP Control Panel and click the Start buttons next to Apache and MySQL:

TEXT 📖 Display only
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.

⚠️ Warning: If localhost doesn't open, the most common cause is a port conflict (e.g., Skype or another app using port 80). In the XAMPP Control Panel, change the Apache port: Config → Apache (httpd.conf) → search for "Listen 80" → change to "Listen 8080" → restart Apache.


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:

TEXT 📖 Display only
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
<?php
echo "Hello, PHP!";
echo "<br>";
echo "Today is " . date("F j, Y");
echo "<br>";

// Display PHP version and configuration info
phpinfo();
?>
▶ Try it Yourself

Output:

TEXT 📖 Display only
Output displayed

Save the file, then visit http://localhost/myphp/ in your browser. You should see:

Congratulations! You've successfully run your first PHP program.

💡 Tip: 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 ?>
PHP
<?php
$name = "John";
$age = 18;
echo "My name is {$name} and I'm {$age} years old.";
▶ Try it Yourself
💡 Tip: Omitting the closing ?> 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

TEXT 📖 Display only
┌──────────┐   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
<?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";
}
▶ Try it Yourself

Output:

TEXT 📖 Display only
Output displayed

❓ FAQ

Q Why does visiting localhost download the .php file instead of showing the PHP execution result?
A Apache's PHP module isn't loaded correctly. Check that Apache shows a green "Running" status in the XAMPP Control Panel, then confirm that httpd.conf contains the LoadModule php_module configuration line.
Q What if port 80 is already in use after installing XAMPP?
A You can change the port. In the XAMPP Control Panel, go to Apache's Config → httpd.conf → search for "Listen 80" → change it to "Listen 8080" → save and restart Apache. After that, access the site at http://localhost:8080/.
Q Mac comes with PHP pre-installed — do I still need XAMPP?
A Mac does ship with PHP, but it's often an older version and doesn't include MySQL or phpMyAdmin. Installing XAMPP or Laravel Herd is recommended — they're much easier to manage.

📖 Summary

📝 Exercises

  1. Install XAMPP and VS Code, start Apache, and visit http://localhost/. Take a screenshot of the XAMPP welcome page.
  2. Inside htdocs/myphp/, create about.php and use echo to output your name, city, and the current time.
  3. Visit http://localhost/myphp/about.php and confirm it works. Take a screenshot of the result.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏