PHP Installation

Setting up your PHP development environment

⚙️ Installing PHP

To run PHP, you need a web server with PHP installed. The easiest way for beginners is using all-in-one packages like XAMPP or WAMP that include Apache, PHP, and MySQL.


<?php
// Test if PHP is working
echo "PHP version: " . phpversion();
?>
                                    

Output:

PHP version: 8.2.0

Installation Options

📦

XAMPP

Cross-platform package with Apache, MySQL, PHP, and Perl. Perfect for beginners on Windows, Mac, or Linux. Easy one-click installation.

Windows Mac Linux
🪟

WAMP

Windows-specific package with Apache, MySQL, and PHP. Simple interface with system tray control. Great for Windows developers starting out.

Windows Only
🍎

MAMP

Mac and Windows package designed for Apple users. Clean interface, easy setup, and includes both Apache and Nginx servers.

Mac Windows
🐧

LAMP

Linux-based stack with Apache, MySQL, and PHP. Installed via command line. Preferred for production servers and Linux enthusiasts.

Linux

🔹 Installing XAMPP (Recommended for Beginners)

Step-by-step guide to install XAMPP on your computer:

Installation Steps:

  1. Download: Visit apachefriends.org and download XAMPP for your OS
  2. Run Installer: Double-click the downloaded file and follow the wizard
  3. Choose Components: Select Apache, MySQL, PHP, and phpMyAdmin
  4. Select Location: Choose installation folder (default: C:\xampp)
  5. Complete Setup: Click Install and wait for completion
  6. Launch Control Panel: Open XAMPP Control Panel
  7. Start Services: Click "Start" for Apache and MySQL

<?php
// Save this as test.php in C:\xampp\htdocs\
echo "<h1>XAMPP is working!</h1>";
echo "<p>PHP Version: " . PHP_VERSION . "</p>";
echo "<p>Server: " . $_SERVER['SERVER_SOFTWARE'] . "</p>";
?>
                            

Output:

XAMPP is working!

PHP Version: 8.2.0

Server: Apache/2.4.54 (Win64)

🔹 Verifying Your Installation

Test if PHP is properly installed and configured:

🔸 Method 1: Create a Test File


<?php
// Save as info.php in your web root folder
phpinfo();
?>
                            

Access: Open browser and visit http://localhost/info.php

You should see a detailed PHP configuration page with version, modules, and settings.

🔸 Method 2: Command Line Check


# Open terminal/command prompt and type:
php -v

# Output shows PHP version:
# PHP 8.2.0 (cli) (built: Dec  6 2022 15:31:23)
                            

🔹 Setting Up Your Workspace

Organize your PHP development environment:

File Locations:

  • XAMPP (Windows): C:\xampp\htdocs\
  • XAMPP (Mac): /Applications/XAMPP/htdocs/
  • WAMP: C:\wamp64\www\
  • MAMP: /Applications/MAMP/htdocs/

Creating Your First Project:

  1. Navigate to your htdocs folder
  2. Create a new folder: "myproject"
  3. Inside, create "index.php"
  4. Access via: http://localhost/myproject/

<?php
// index.php - Your first project file
echo "<h1>My First PHP Project</h1>";
echo "<p>Welcome to my website!</p>";

// Display current date
$today = date("F j, Y");
echo "<p>Today is: $today</p>";
?>
                            

Output:

My First PHP Project

Welcome to my website!

Today is: October 2, 2025

🔹 Common Installation Issues

Troubleshooting tips for common problems:

Port Already in Use:

Problem: Apache won't start because port 80 is occupied

Solution: Change Apache port to 8080 in httpd.conf or stop conflicting programs (Skype, IIS)

PHP Not Executing:

Problem: Browser shows PHP code instead of executing it

Solution: Ensure Apache is running and you're accessing via localhost, not file://

MySQL Won't Start:

Problem: MySQL service fails to start

Solution: Check if port 3306 is free, or change MySQL port in my.ini

🔹 Recommended Code Editors

Choose a good editor for writing PHP code:

Visual Studio Code

Free, powerful, with PHP extensions. Best choice for most developers. Includes debugging and IntelliSense.

PhpStorm

Professional PHP IDE with advanced features. Paid but offers student licenses. Best for large projects.

Sublime Text

Fast, lightweight editor with PHP syntax highlighting. Great for quick edits and smaller projects.

Notepad++

Simple Windows editor with PHP support. Good for beginners learning the basics without complexity.

🔹 Testing Your Setup

Create a comprehensive test file to verify everything works:


<?php
// complete-test.php
echo "<h2>PHP Installation Test</h2>";

// Version check
echo "<p><strong>PHP Version:</strong> " . phpversion() . "</p>";

// Server info
echo "<p><strong>Server:</strong> " . $_SERVER['SERVER_SOFTWARE'] . "</p>";

// Document root
echo "<p><strong>Document Root:</strong> " . $_SERVER['DOCUMENT_ROOT'] . "</p>";

// Test calculation
$result = 5 + 3;
echo "<p><strong>Math Test:</strong> 5 + 3 = $result</p>";

// Test date function
echo "<p><strong>Date Function:</strong> " . date("Y-m-d H:i:s") . "</p>";

echo "<p style='color: green;'>✓ All tests passed! PHP is working correctly.</p>";
?>
                            

Output:

PHP Installation Test

PHP Version: 8.2.0

Server: Apache/2.4.54 (Win64)

Document Root: C:/xampp/htdocs

Math Test: 5 + 3 = 8

Date Function: 2025-10-02 14:30:45

✓ All tests passed! PHP is working correctly.

🧠 Test Your Knowledge

What is the default folder for PHP files in XAMPP?