Skip to main content
PHP for Beginners
CHAPTER 02 Beginner

Setting Up PHP Development Environment

Updated: May 12, 2026
15 min read

# Chapter 2: Setting Up PHP Development Environment

1. Introduction

Welcome to Chapter 2! Unlike HTML, which can be run simply by double-clicking the file in your browser, PHP requires a web server and a PHP processor to run. Setting up this environment locally (on your own computer) allows you to build and test websites without needing to pay for web hosting. In this chapter, we will learn how to install the necessary tools, structure our files, and write our first local PHP script.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Understand what local server environments like XAMPP and WAMP are.
  • Install XAMPP on your computer.
  • Understand how to structure PHP files within the server directory.
  • Access your local server via localhost.
  • Set up Visual Studio Code (VS Code) for PHP development.
  • Deploy basic files to shared hosting (e.g., Hostinger).

3. Installing XAMPP

XAMPP stands for Cross-Platform, Apache (the server), MariaDB (the database), PHP, and Perl. It is the easiest way to get a local development environment running.
  1. 1. Download XAMPP from the official Apache Friends website.
  1. 2. Run the installer and accept the default settings.
  1. 3. Open the XAMPP Control Panel.
  1. 4. Click the "Start" button next to Apache. If you plan to use a database later, start MySQL as well.
Once Apache is highlighted in green, your local web server is running!

4. Installing WAMP

WAMP is similar to XAMPP but is designed specifically for Windows (Windows, Apache, MySQL, PHP). It functions the exact same way. XAMPP is generally preferred for its cross-platform compatibility, but WAMP is a great alternative if you are solely on a Windows machine.

5. PHP File Structure and Localhost Basics

Where do you save your PHP files?
  • If using XAMPP, navigate to C:\xampp\htdocs.
  • If using WAMP, navigate to C:\wamp\www.

The htdocs (or www) folder is the root of your web server. Any folder or file you create here can be accessed in your browser by typing http://localhost.

Example: If you create a folder named mywebsite in htdocs, and place an index.php file inside it, you can view it in your browser by navigating to: http://localhost/mywebsite/index.php (or just http://localhost/mywebsite as index.php is the default file).

6. VS Code Setup

Visual Studio Code is the industry standard code editor.
  1. 1. Download and install VS Code.
  1. 2. Open VS Code and go to File > Open Folder. Select your htdocs folder (or the specific project folder inside it).
  1. 3. Install the PHP Intelephense extension for powerful auto-completion and error checking.
  1. 4. Install the Prettier or PHP CS Fixer extension to keep your code cleanly formatted.

7. Hostinger Setup (Live Deployment)

When you are ready to show your site to the world, you need web hosting. Hostinger and cPanel-based hosts make this easy.
  1. 1. Log in to your hosting account and navigate to the File Manager.
  1. 2. Find the public_html folder. This is the live equivalent of your local htdocs folder.
  1. 3. Upload your .php files into public_html.
  1. 4. Your website is now live on your domain name!

8. Syntax Explanation & Real-World Example

Let's create the default structure of a PHP file. Remember, PHP files can contain plain HTML alongside PHP code.
php
123456789101112
<!DOCTYPE html>
<html>
<head>
    <title>Environment Test</title>
</head>
<body>
    <?php
        // PHP block embedded inside HTML
        echo "<h1>XAMPP is working perfectly!</h1>";
    ?>
</body>
</html>

9. Output Explanations

When you save the above code as index.php in htdocs/test and go to localhost/test, the Apache server reads the PHP block, executes the echo command, and outputs the <h1> tag. The browser then renders a large, bold heading reading "XAMPP is working perfectly!".

10. Common Mistakes

  • Saving files outside htdocs: PHP files will NOT run if you save them to your Desktop or Documents folder.
  • Forgetting to start Apache: If you get a "Site cannot be reached" error on localhost, you probably forgot to click "Start" on Apache in the XAMPP Control Panel.
  • Port Conflicts: Sometimes Skype or other programs use Port 80 (Apache's default). You may need to change Apache's port in the XAMPP config if it refuses to start.

11. Best Practices

  • Always create a new subfolder inside htdocs for each new project to keep things organized.
  • Use index.php as your main entry file, as servers automatically look for it.
  • Keep your XAMPP installation updated, but backup your databases and files before doing so.

12. Exercises

  1. 1. Install XAMPP and start the Apache server.
  1. 2. Create a folder named practice inside htdocs.
  1. 3. Create a file named hello.php and write a script to output "Hello from localhost!".
  1. 4. View the file in your browser.

13. Mini Project: Create First PHP Webpage

Task: Create a complete webpage with a header, a main content area, and a footer, using PHP to generate dynamic content.
php
1234567891011121314151617181920212223242526272829
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>My First PHP Project</title>
    <style>
        body { font-family: Arial; margin: 40px; }
        .footer { margin-top: 50px; color: gray; }
    </style>
</head>
<body>
    <header>
        <h1>
            <?php echo "Welcome to My Awesome Website"; ?>
        </h1>
    </header>

    <main>
        <p>This paragraph is pure HTML.</p>
        <p>
            <?php echo "This paragraph was generated by the PHP engine!"; ?>
        </p>
    </main>

    <footer class="footer">
        <p>© <?php echo "2026"; ?> My PHP Journey.</p>
    </footer>
</body>
</html>

14. Coding Challenges

Challenge 1: Modify the mini project to add a new <div> where PHP echoes out your favorite programming language.
php
1234
<?php
// Solution snippet to add inside <main>
echo "<div>My favorite language so far is PHP!</div>";
?>

15. MCQs with Answers

1. What folder do you place your files in when using XAMPP? A) C:\xampp\www B) C:\xampp\public C) C:\xampp\htdocs D) C:\xampp\php *Answer: C*

2. What does the "A" in XAMPP stand for? A) Application B) Apache C) Array D) Advanced *Answer: B*

3. What URL do you type in your browser to access files in htdocs? A) http://www.local.com B) http://localhost C) http://mycomputer D) http://xampp *Answer: B*

16. Interview Questions

Q: What happens if you try to open a .php file directly in your browser without a local server? *A:* The browser will either display the raw PHP source code or prompt you to download the file. It will not execute the code because the browser lacks the PHP processing engine; it relies on a web server like Apache to handle the execution.

Q: Explain the role of Apache in the XAMPP stack. *A:* Apache is the web server software. It listens for HTTP requests from the browser, locates the requested files, passes .php files to the PHP engine for processing, and then sends the generated HTML back to the client.

17. FAQs

Q: Can I use macOS for PHP development? *A:* Yes! You can use MAMP (Mac, Apache, MySQL, PHP) or Laravel Valet, which are excellent alternatives for macOS users. XAMPP also has an OS X version.

Q: Do I need internet access to use localhost? *A:* No. Localhost operates entirely on your own computer, meaning you can develop websites offline.

18. Summary

In this chapter, you successfully set up a professional PHP development environment. You installed XAMPP, learned how to navigate the htdocs directory, and accessed your local server via localhost. You also wrote and viewed your first dynamic webpage using VS Code.

19. Next Chapter Recommendation

With your environment ready, it is time to dive deep into writing code! In Chapter 3: PHP Syntax and Basic Structure, we will explore exactly how PHP is written, how to leave comments, and the different ways to output data to the screen.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·