CHAPTER 03
Beginner
Setting Up PHP and Local Development Environment
Updated: May 14, 2026
15 min read
# CHAPTER 3
Setting Up PHP and Local Development Environment
1. Introduction
You cannot run PHP code by simply double-clicking a file like you can with an HTML file. PHP requires a Web Server (to intercept HTTP requests) and a PHP Engine (to process the code). Since you don't want to buy a live internet server just to learn, we will turn your personal computer into a "Local Server." In this chapter, we will install and configure XAMPP, providing you with a fully functional local development environment.2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the components of a LAMP/XAMPP stack.
- Install XAMPP on your local machine.
- Start a local Apache web server and MySQL database.
- Navigate phpMyAdmin for database management.
- Create and run your first PHP file locally.
3. Beginner-Friendly Explanation
If you try to open a.php file in Chrome directly from your Desktop (e.g., file:///C:/Users/Desktop/index.php), the browser will just show you the raw code. It doesn't know how to run it.
You need a Web Server.
XAMPP is a free, all-in-one software package. Think of it as an "Internet Simulator." When you turn on XAMPP, your laptop pretends it is a powerful server in a massive data center. You can type http://localhost into your browser, and your browser will talk to your laptop's internal server, allowing the PHP code to run properly.
4. What is XAMPP?
XAMPP stands for:- X: Cross-Platform (works on Windows, Mac, Linux).
- A: Apache (The Web Server software).
- M: MariaDB/MySQL (The Database).
- P: PHP (The programming language).
- P: Perl (Another language, rarely used by beginners).
5. Installing XAMPP
Step-by-Step Installation:-
1.
Go to
apachefriends.organd download XAMPP for your OS.
- 2. Run the installer. Leave all the default settings checked.
-
3.
Install it in the default directory (usually
C:\xamppon Windows or/Applications/XAMPPon Mac).
- 4. Open the XAMPP Control Panel.
- 5. Click Start next to Apache and MySQL. (If they turn green, your local server is running!).
6. Running Local Servers (htdocs)
Where do you put your code?
Apache only looks for files in one specific folder.
-
Windows:
C:\xampp\htdocs\
-
Mac:
/Applications/XAMPP/htdocs/
Creating Your First App:
-
1.
Open the
htdocsfolder.
-
2.
Create a new folder named
myproject.
-
3.
Inside
myproject, create a file namedindex.php.
-
4.
Open your web browser and type:
http://localhost/myproject/index.php
7. Coding Tutorial: Your First Local Script
Openindex.php in your code editor (like VS Code) and add this code:
php
Save the file and refresh your browser (http://localhost/myproject/index.php). You will see a massive purple page displaying all your PHP configuration details. Your local environment works!
8. Using phpMyAdmin
XAMPP comes with a database management tool called phpMyAdmin.-
1.
Open your browser and type:
http://localhost/phpmyadmin
- 2. This provides a visual interface (frontend) for your MySQL database.
- 3. You can click "New" to create databases, tables, and insert data without having to write complex SQL code in a command-line terminal.
9. Best Practices
- Use a Code Editor: Do not use Notepad. Download a professional Code Editor like Visual Studio Code (VS Code). It provides syntax highlighting for PHP, autocompletion, and helps catch errors before you run the code.
10. Common Mistakes
-
Forgetting to Start Apache: If your browser says "Site cannot be reached" when you go to
localhost, you probably forgot to open the XAMPP Control Panel and click "Start" on Apache.
-
Double-Clicking the File: Never double-click a
.phpfile in your file explorer to open it. It must be accessed through the browser using thehttp://localhost/URL.
11. Exercises
-
1.
Start your XAMPP server, create a folder called
testappin thehtdocsdirectory, and write a PHP script that prints the phrase "Server is running!" to the browser.
12. Coding Challenges
-
Challenge: Explore
http://localhost/phpmyadmin. Create a new database namedtestdband a table nameduserswith two columns:idandname.
13. MCQs with Answers
Question 1
In the XAMPP stack, what role does Apache play?
Question 2
Where must you save your PHP files in a default Windows XAMPP installation for Apache to serve them to the browser?
14. Interview Questions
- Q: What is a LAMP/XAMPP stack, and describe the specific function of each component (Linux, Apache, MySQL, PHP).
-
Q: Explain why opening a
.phpfile directly in a browser via thefile://protocol results in raw code display, whereas accessing it viahttp://localhost/executes the script.
15. FAQs
Q: Can other people on the internet see mylocalhost website?
A: No. localhost is completely private to your specific computer. It is a closed loop. No one on the internet can see your work until you purchase a domain name and upload your code to a live hosting provider (covered in Chapter 18).
16. Summary
In Chapter 3, we transformed your computer into a backend development laboratory. By installing XAMPP, you now possess a local Apache Web Server, a MySQL Database, and the PHP Engine. By saving your files in thehtdocs directory and accessing them via http://localhost, you can write, test, and debug powerful backend applications in a completely safe, private environment.