Lesson 1.2: Setting Up Your Development Environment

Your PHP Workshop

Time to build your development environment. Think of this like setting up a professional kitchen before cooking. You need the right tools, properly configured, ready to create amazing things.

Here's what we're installing:

Don't worry if these terms sound foreign. By lesson's end, you'll have everything running smoothly.

Operating System Considerations

PHP runs everywhere: Windows, macOS, Linux. However, setup differs slightly. I'll cover all three operating systems. Skip to your relevant section, though reading all three helps understand PHP's flexibility.

One crucial note: PHP development happens most naturally on Unix-like systems (macOS and Linux). Windows works fine, but you'll occasionally hit quirks. If you're on Windows, consider using WSL2 (Windows Subsystem for Linux) for a smoother experience.

Installing PHP

Windows Installation

Windows doesn't include PHP by default. Let's fix that.

Option 1: XAMPP (Easiest)

  1. Download XAMPP - choose the latest PHP 8.x version
  2. Run the installer (ignore antivirus warnings - they're false positives)
  3. Select components: Apache, MySQL, PHP, phpMyAdmin
  4. Install to C:\xampp (avoid Program Files - spaces cause issues)
  5. Launch XAMPP Control Panel
  6. Start Apache and MySQL

That's it! PHP now runs at http://localhost.

Option 2: Native PHP (More Control)

  1. Visit windows.php.net
  2. Download the Thread Safe x64 zip
  3. Extract to C:\php
  4. Add C:\php to your system PATH:
  1. Rename php.ini-development to php.ini
  2. Open command prompt, type php -v

See version info? Success!

macOS Installation

macOS includes PHP, but it's usually outdated. Let's install modern PHP properly.

Using Homebrew (Recommended)

# Install Homebrew if you haven't
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# Install PHP
brew install php

# Verify installation
php -v

Alternative: MacPorts

# Install MacPorts from macports.org
# Then run:
sudo port install php82 +apache2

# Select PHP version
sudo port select --set php php82

Linux Installation

Linux makes PHP installation trivial. Commands vary by distribution:

Ubuntu/Debian:

sudo apt update
sudo apt install php8.2 php8.2-cli php8.2-common php8.2-mysql php8.2-xml php8.2-curl php8.2-mbstring

# Verify
php -v

Fedora/CentOS/RHEL:

sudo dnf install php php-cli php-common php-mysqlnd php-xml php-curl php-mbstring

# Verify
php -v

Arch Linux:

sudo pacman -S php

# Verify
php -v

Choosing Your Code Editor

Your editor shapes your coding experience. Choose wisely.

VS Code dominates PHP development for good reasons. It's free, fast, and incredibly powerful.

  1. Download from code.visualstudio.com
  2. Install these essential extensions:

Configure PHP executable path:

  1. Open Settings (Ctrl/Cmd + ,)
  2. Search "PHP executable"
  3. Set path:

PHPStorm (Professional Choice)

JetBrains PHPStorm offers unmatched PHP intelligence. It's paid ($89/year) but offers a 30-day trial. Students get it free through GitHub Student Pack.

PHPStorm advantages:

Other Options

Start with VS Code. You can always switch later.

Setting Up a Local Server

PHP needs a web server to shine. Let's explore options.

Built-in PHP Server (Quickest)

PHP includes a development server. Perfect for learning:

# Navigate to your project folder
cd /path/to/your/project

# Start server
php -S localhost:8000

# Visit http://localhost:8000 in your browser

Limitations: Single-threaded, development only. Never use in production!

XAMPP/WAMP/MAMP (Beginner-Friendly)

These packages bundle Apache, MySQL, and PHP:

Already installed XAMPP? Your documents go in:

Docker (Modern Approach)

Docker containerizes your environment. Steeper learning curve, but worth it:

  1. Install Docker Desktop
  2. Create docker-compose.yml:
version: "3.8"
services:
  php:
    image: php:8.2-apache
    ports:
      - "8080:80"
    volumes:
      - ./src:/var/www/html
  1. Run docker-compose up
  2. Access at http://localhost:8080

Laravel Valet (macOS Only)

Valet offers zero-configuration development:

brew install php
composer global require laravel/valet
valet install
cd ~/Sites
valet park

Any folder in ~/Sites becomes http://foldername.test automatically!

Installing Composer

Composer manages PHP dependencies. Think npm for PHP. You'll use it constantly.

Windows Installation

  1. Download Composer-Setup.exe
  2. Run installer (it finds PHP automatically)
  3. Open new command prompt
  4. Type composer --version

macOS/Linux Installation

# Download installer
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

# Verify installer (optional but recommended)
php -r "if (hash_file('sha384', 'composer-setup.php') === 'dac665fdc30fdd8ec78b38b9800061b4150413ff2e3b6f88543c636f7cd84f6db9189d43a81e5503cda447da73c7e5b6') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

# Install globally
php composer-setup.php --install-dir=/usr/local/bin --filename=composer

# Clean up
php -r "unlink('composer-setup.php');"

# Verify
composer --version

Creating Your First PHP File

Everything's installed? Let's test it!

  1. Create a new folder called php-course
  2. Inside, create index.php:
<?php
phpinfo();
  1. Run your chosen server:
  1. Open your browser to the appropriate URL

See a purple PHP information page? Congratulations! PHP works.

Understanding phpinfo()

That phpinfo() page contains valuable information:

Bookmark this function. When debugging server issues, phpinfo() provides crucial details.

Configuring PHP

PHP's behavior lives in php.ini. Find yours:

Key settings to adjust for development:

; Display errors (NEVER in production!)
display_errors = On
error_reporting = E_ALL

; Increase memory for complex scripts
memory_limit = 256M

; Extend execution time for debugging
max_execution_time = 60

; Show startup errors
display_startup_errors = On

; Log errors for later review
log_errors = On
error_log = /path/to/error.log

Save changes, restart your web server.

File Organization

Establish good habits early. Here's a sensible structure:

php-course/
├── module-01/
│   ├── lesson-01/
│   ├── lesson-02/
│   └── project/
├── module-02/
│   ├── lesson-01/
│   └── ...
├── assets/
│   ├── css/
│   ├── js/
│   └── images/
└── README.md

Each lesson gets its own folder. Projects live separately. Assets stay organized.

Testing Your Setup

Create test.php in your project folder:

<?php
// Variables
$name = "PHP Developer";
$version = phpversion();

// Array
$skills = ['HTML', 'CSS', 'JavaScript', 'PHP'];

// Add another skill
$skills[] = 'MySQL';

// Current date
$today = date('l, F j, Y');
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>PHP Setup Test</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
            background-color: #f4f4f4;
        }
        .success {
            background-color: #4CAF50;
            color: white;
            padding: 15px;
            border-radius: 5px;
        }
        .info {
            background-color: #2196F3;
            color: white;
            padding: 10px;
            margin: 10px 0;
            border-radius: 3px;
        }
    </style>
</head>
<body>
    <div class="success">
        <h1>✓ PHP is working!</h1>
    </div>

    <h2>Hello, <?php echo $name; ?>!</h2>

    <div class="info">
        <strong>PHP Version:</strong> <?php echo $version; ?>
    </div>

    <div class="info">
        <strong>Today's Date:</strong> <?php echo $today; ?>
    </div>

    <h3>Your skillset includes:</h3>
    <ul>
        <?php foreach ($skills as $skill): ?>
            <li><?php echo $skill; ?></li>
        <?php endforeach; ?>
    </ul>

    <p>If you can see this page with dynamic content, your PHP environment is properly configured!</p>
</body>
</html>

Run this file through your web server. See personalized content? Perfect! Your environment works.

Troubleshooting Common Issues

"PHP is not recognized" error:

Blank white page:

"Port already in use" error:

Extension missing errors:

File not found errors:

Development Workflow Tips

Use Version Control: Install Git. Track changes from day one:

git init
git add .
git commit -m "Initial setup"

Browser DevTools: Learn them! F12 opens developer tools. Network tab shows requests. Console displays JavaScript errors.

Multiple Browsers: Test in Chrome, Firefox, Safari, Edge. Each renders slightly differently.

Code Formatting: Configure your editor for PSR-12 standard. Consistent code reads easier.

Backup php.ini: Before modifying configuration, copy php.ini to php.ini.backup.

Security Notes

Development environments prioritize convenience over security. NEVER use these settings in production:

We'll cover production configuration later. For now, keep development servers local only.

Next Steps

Your environment is ready! You can:

Take a moment to celebrate. Setting up development environments frustrates many beginners. You pushed through!

Try modifying test.php:

Experimentation builds confidence. Break things. Fix them. That's learning!

Summary

You've accomplished a lot:

Your development environment matches what professionals use. The only difference? Experience. Let's start building that experience.

Ready to write your first real PHP script? Lesson 1.3 awaits!


Quick Reference

Start/Stop Services:

Important Paths:

Useful Commands:

VS Code Shortcuts: