If you’re learning PHP or building your first web application, one of the first challenges is running PHP code on your own computer. Since PHP is a server-side scripting language, it cannot run directly in a browser like HTML or JavaScript. Instead, it requires a server environment such as Apache or Nginx that can process PHP scripts.
In this guide, we’ll cover everything you need to know about running PHP locally, including setting up XAMPP, resolving common issues, and testing your first PHP file.
Why Do You Need a Local Server for PHP?
PHP is designed to run on a server. When you open a PHP file in your browser without a server, the code will not be executed—it will just show up as plain text. A local server environment solves this problem by creating a mini web server on your computer, allowing you to test and run PHP code just like you would on a live hosting server.
Some popular tools for running PHP locally include:
- XAMPP – A beginner-friendly package that includes Apache, PHP, MySQL, and phpMyAdmin.
- WAMP (Windows) / MAMP (Mac) – Local development environments with PHP and MySQL support.
- Docker – A more advanced solution for containerized environments.
For beginners, XAMPP is the easiest option, so that’s what we’ll use in this tutorial. Download XAMPP and install on your computer.
Download XAMPP and Install on Your Computer
Go to the official XAMPP website and download the installer suitable for your operating system (Windows, macOS, or Linux). Once downloaded, run the installer and follow the setup wizard. The installation will include Apache (server), PHP (scripting language), MySQL (database), and phpMyAdmin (database manager)—everything you need to run PHP code locally.
Step 1: Download and Install XAMPP
- Visit the official XAMPP website and download the installer for your operating system (Windows, macOS, or Linux).
- Run the installer and follow the setup wizard.
- Once installed, open the XAMPP Control Panel.
From here, you’ll see options to start/stop different modules like Apache (web server) and MySQL (database server).
Step 2: Start Apache (and MySQL if Needed)
- Click Start next to Apache in the XAMPP control panel.
- If your project involves databases, also start MySQL.
⚠️ Common Issue: If Apache doesn’t start, it usually means another program is using Port 80 (commonly Skype, TeamViewer, or IIS).
How to Fix Port 80 Conflict:
- Check if Skype or TeamViewer is running.
- In Skype, go to:
Tools → Options → Advanced → Connection
Uncheck: Use port 80 and 443 for additional incoming connections. - Restart Apache in XAMPP.
Step 3: Locate the htdocs Folder
When XAMPP is installed, it creates a directory called htdocs inside the XAMPP installation folder. This is your web root directory where you must place all PHP files to run them in your browser.
Example path (Windows):
C:\xampp\htdocs\
Any file saved here can be accessed in your browser using:
http://localhost/filename.php
- or
http://127.0.0.1/filename.php
Step 4: Create Your First PHP File
- Open a text editor (Notepad++, VS Code, Sublime Text).
- Create a new file named
mytest.php
. - Add the following code:
- Save this file inside the htdocs folder.
<?php
phpinfo();
echo "My PHP is running successfully! 🎉";
?>
Step 5: Run Your PHP Code in Browser
Now, open your browser and type:
http://127.0.0.1/mytest.php
If PHP is installed correctly, you’ll see:
- A full PHP information page generated by
phpinfo()
. - The message: My PHP is running successfully! 🎉
Congratulations! 🎊 You have successfully run PHP code on your computer.
Troubleshooting Common Errors
- Apache Won’t Start: Check port conflicts (Skype, IIS, TeamViewer).
- Page Not Found (404): Make sure the file is in the
htdocs
directory. - Blank Page: Check if your file has the correct
.php
extension, not.html
. - PHP Errors: Enable error reporting by adding this at the top of your file:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
What’s Next?
Now that you know how to run PHP code locally, you can:
- Learn the basics of PHP arrays and functions
- Start working with databases using MySQL and phpMyAdmin
- Explore PHP frameworks like Laravel or CodeIgniter
- Build real-world projects such as login systems, blogs, or e-commerce websites
Final Thoughts
Setting up a local PHP development environment is the first step toward becoming a web developer. With XAMPP, you can run PHP scripts, test applications, and build database-driven websites right from your computer without needing a live server.
Once you’re comfortable with PHP basics, you can deploy your projects to an actual web hosting server for the world to see.