View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All
View All

How to Use XAMPP For PHP Files? (Stepwise Approach)

By Mukesh Kumar

Updated on Apr 17, 2025 | 17 min read | 1.1k views

Share:

As of January 2025, PHP 7.4 remains the most widely used version, running on 7.6% of all PHP websites. When setting up XAMPP, be sure to check which PHP version is installed, as certain PHP versions may have specific compatibility with your scripts.

To run PHP files in XAMPP, you need to set up a local server environment using XAMPP, which stands for Apache, MySQL, PHP, and Perl. It’s an essential tool for testing and running PHP code locally. 

If you're new to XAMPP for PHP, this guide will walk you through each step. By the end, you’ll know exactly how to run PHP code in XAMPP and set up your development environment seamlessly. 

How to Run PHP Files in XAMPP? 

PHP is a popular server-side scripting language that powers many dynamic websites and applications. It runs on the server and can generate HTML content that is sent to the user’s browser. 

XAMPP, which stands for Apache, MySQL, PHP, and Perl, is a local development environment that makes it easy to test and run PHP files on your computer. By using XAMPP for PHP, you don’t need to rely on an external server or internet connection for development.

What is PHP?

PHP is a server-side scripting language, meaning that it runs on the server to generate dynamic content for web pages. Unlike HTML, which is static, PHP allows you to create interactive and database-driven websites. Whether it’s displaying content based on user input or processing form data, PHP is essential for web development.

PHP’s ability to interact with databases makes it powerful for building websites where content changes frequently. 

PHP integrates seamlessly with modern frameworks like Laravel, making it ideal for building robust, scalable web applications. Websites like WordPress, Facebook, and Wikipedia all use PHP on the server side to create personalized and dynamic experiences for users.

PHP Syntax and Basics

PHP syntax is simple and easy to understand. You write PHP code between <?php ?> tags, and this code will be executed on the server. 

Here’s a basic example of PHP syntax: 

<?php
  echo "Hello, World!";
?>

Output:

Hello, World!

In this script, echo is a PHP statement that outputs text to the web page. 

The output will be displayed as "Hello, World!" in the browser when you run the script. 

If you’re learning how to run PHP code in XAMPP, simply create a PHP file with this code and place it in the htdocs folder in XAMPP. 

Then, access it by going to http://localhost/your-file-name.php in your browser. This is the first step in how to run PHP file in XAMPP.

If you're looking to deepen your understanding of web development and run PHP code efficiently, grasping the right tools is key. Check out upGrad's Software Engineering courses and enroll today to start building skills for your career goals.

What is XAMPP?

XAMPP is a free, open-source platform that allows you to run ApacheMySQL, PHP, and Perl on your local machine. It allows developers to create a local server environment where they can test and run PHP code without needing an actual web server.

When you install XAMPP, it provides a complete solution that includes everything you need to run PHP locally, including a web server (Apache), a database server (MySQL), and a scripting language (PHP). 

The best part? It’s easy to set up and works on all major operating systems like Windows, macOS, and Linux. 

By using XAMPP for PHP, you can get started with web development without complicated setups or dependencies.

If you're ready to move beyond basic web development, the 13-month Executive Diploma in Machine Learning and AI with IIIT-B course by upGrad will help you do just that. Join 10k+ successful ML professionals and apply your skills to real-life projects.

How to Set Up PHP and XAMPP?

Setting up PHP with XAMPP is a straightforward process that allows you to start running PHP files on your local machine. You’ll walk through the steps needed to download, install, and configure XAMPP, followed by testing it with a simple PHP script. 

System Requirements for XAMPP

Before you begin, it’s important to know if your system meets the necessary requirements for installing XAMPP. Here are the key things to check:

  • Operating System Compatibility: XAMPP works on Windows, macOS, and Linux. Make sure you’re downloading the correct version for your operating system. You can find the download links on the XAMPP official website.
  • Hardware Requirements: XAMPP isn’t resource-heavy, but your computer should have at least:
    • 2 GB of RAM (recommended)
    • 2 GB of free disk space to store the XAMPP software and your PHP projects
  • Software Prerequisites: Ensure you have a supported version of your operating system. XAMPP is compatible with Windows 7 and above, macOS 10.6 or later, and most modern Linux distributions. Also, be sure to have administrative rights on your computer for installation.

For learning and small-scale projects, XAMPP works great. However, for production environments, consider using a dedicated server setup.

How to Download and Install XAMPP?

Now that you know your system meets the requirements, let’s walk through downloading and installing XAMPP.

Step 1: Download XAMPP from the Official Website

  • Go to the XAMPP official website.
  • Select the correct version for your operating system (Windows, macOS, or Linux).
  • Click the Download button for your OS. The download will start automatically.

Step 2: Installation Process with Configurations

  • Once the installer is downloaded, open the file to begin the installation process.
  • Launching the Installer: Follow the setup wizard and choose the components you want to install. For PHP development, you only need to select Apache, MySQL, and PHP. Apache is the web server that serves your PHP files, MySQL is the database server for dynamic content, and PHP is the scripting language that runs your server-side code. These are the core components of XAMPP full form in PHP.
  • Default or Custom Installation: If you’re unsure, select the default installation to keep things simple. If you want to customize, you can change the installation path or select additional components (like Perl).
  • Once you've made your selections, click Next and proceed with the installation. The process should only take a few minutes.

Step 3: Verify the Installation

  • After installation, you can verify that XAMPP is working by launching the XAMPP Control Panel.
  • Open the control panel and click Start next to Apache and MySQL. These are the two services you need to run to use PHP and databases locally.
  • Once both services are running, open a web browser and type http://localhost into the address bar. If you see the XAMPP dashboard, then the installation is successful!

Now, let’s test PHP. Open a text editor, create a file named test.php, and add the following code:

<?php
  phpinfo();
?>
  • Save this file in the htdocs folder inside your XAMPP installation directory (usually found at C:\xampp\htdocs on Windows). This folder is essential because the browser accesses PHP files from it via http://localhost.
  • To view the file, open your browser and go to http://localhost/test.php. If everything is set up correctly, you should see a page with detailed information about your PHP setup, confirming that you have successfully configured how to run PHP in XAMPP.

With PHP and XAMPP up and running, you're now equipped to dive into your first PHP project. XAMPP provides a local environment where you can seamlessly build and test your code without the need for an external server. 

Coverage of AWS, Microsoft Azure and GCP services

Certification8 Months

Job-Linked Program

Bootcamp36 Weeks

Setting Up Your First PHP Project in XAMPP

Let’s get your first PHP project up and running with XAMPP. You’ll go through the process of creating a project folder, adding your first PHP file, and running it locally on your computer. 

You'll learn how to organize your files and run PHP code in XAMPP, which will serve as the foundation for all your future PHP projects. It’s straightforward, hands-on, and the perfect next step to start coding with PHP.

Creating Your PHP Project Folder

Every project you create in XAMPP should reside in the htdocs folder. This folder is the document root for XAMPP, meaning any files you place here can be accessed through your local server.

Navigating to the htdocs Folder

The htdocs folder is where all your PHP files will live. Here’s how you can locate it:

  • Windows: Navigate to C:\xampp\htdocs
  • macOS: Open /Applications/XAMPP/htdocs
  • Linux: Find it inside the opt/lampp/htdocs directory

This folder is the entry point for your local server, which means any PHP file stored here can be executed when you access it through your browser via 

http://localhost/your-file-name.php

Creating a New Project Directory

To keep your work organized, it’s best to create a new folder for every project inside the htdocs folder.

  • Open the htdocs folder in the file explorer.
  • Create a new folder for your project, e.g., MyFirstPHPProject.
  • This folder will contain all the PHP files you need for your project.

Naming Conventions: Stick to simple and descriptive names for your folders, like MyFirstPHPProject, to make it easy to identify later. 

Avoid spaces and special characters—use hyphens (-) or underscores (_) instead.

Adding PHP Files to the Project

Now that you have a folder for your project, it’s time to create your first PHP file. This is where you'll test if your XAMPP setup is working correctly.

  1. Create the index.php File:
    • Inside your project folder (MyFirstPHPProject), create a new text file and name it index.php.
    • The index.php file will be the main file for your project and can be accessed directly at http://localhost/MyFirstPHPProject/index.php.
  2. Write a Simple PHP Script:
    • Open the index.php file in any text editor (like Notepad on Windows, TextEdit on macOS, or Sublime Text).
    • Add the following PHP code to test your setup:
<?php
  echo "Hello, XAMPP is working!";
?>
  • This script uses echo to display a simple message in your browser. This will confirm whether how to run PHP code in XAMPP is working correctly.

Running Your PHP File in XAMPP

  • To see your PHP file in action, open your browser and type http://localhost/MyFirstPHPProject/index.php in the address bar.
  • You should see the message: "Hello, XAMPP is working!" if everything is set up correctly.

This simple process of creating a PHP project folder and adding your first PHP script is the foundation for any future projects you build.

Also Read: 20 Best PHP Project Ideas & Topics For Beginners [2025]

Let’s go beyond the basics and see how to manage and scale your PHP projects in XAMPP, starting with getting your server up and running. 

How to Start a New PHP Project in XAMPP?

Now that you’ve created your first PHP project, it’s time to run it using XAMPP. You’ll learn how to start the server, manage your services, and troubleshoot any issues you might face along the way. 

Starting the XAMPP Server

Before you can run any PHP files, you need to start the XAMPP server. This is a simple process, but it's crucial to ensure everything is running smoothly.

Launching XAMPP Control Panel

The XAMPP control panel is where you manage the server and database services. Here’s how to launch it:

  • Windows: Go to the XAMPP folder (usually in C:\xampp\) and click on xampp-control.exe.
  • macOS: Open XAMPP from the Applications folder.

Once the control panel is open, you’ll see a list of services, including Apache (the web server) and MySQL (the database server). These services need to be running to process and store your PHP content.

  • Start Apache: Click the Start button next to Apache to enable the web server.
  • Start MySQL: Click the Start button next to MySQL if your project requires a database.

Overview of Available Controls: The control panel allows you to start, stop, and configure these services. You can also check the logs for any errors or issues that arise.

Starting Apache and MySQL Services

To run PHP code in XAMPP, Apache needs to be running because it serves your PHP files to the browser. MySQL is required for handling databases if your project uses one.

  1. Click the Start button next to Apache to start the web server.
  2. If your project needs a database, click Start next to MySQL as well.
  3. Verifying Services: After starting the services, you can verify that everything is running by opening your browser and typing http://localhost in the address bar. If you see the XAMPP dashboard, both services are running correctly.

Once the services are up and running, you can begin testing your PHP files. 

Open your browser and enter the path to your project, like http://localhost/MyFirstPHPProject/index.php, and your PHP file should load without issues.

If you're working with databases in XAMPP and want to strengthen your knowledge, learning database design is key. The Introduction to Database Design with MySQL free course by upGrad will teach you to structure and manage databases efficiently. Get started today!

Troubleshooting Common Issues

Sometimes, things don’t go as smoothly as expected, and you might encounter issues when running PHP files. Whether it’s a server not starting, a PHP script not executing correctly, or unexpected error messages showing up in your browser, these problems can be frustrating for beginners. 

Thankfully, most of these issues are common and easily fixable. This section will help you identify and resolve those problems so you can continue your development without roadblocks.

Apache or MySQL Not Starting

If Apache or MySQL isn't starting, there could be several reasons. Skype, for example, often uses port 80 by default, which conflicts with XAMPP's Apache server.

Here’s how you can troubleshoot:

  • Port Conflicts: Apache may not start if another program is using port 80 (the default port for web servers).
  • Permissions: Ensure XAMPP has the required permissions to run. On some systems, you may need administrative rights to start Apache and MySQL.

Step-by-Step Troubleshooting:

  1. Check Logs: Open the XAMPP control panel and click on Logs to see any error messages.
  2. Change Ports: If Apache isn’t starting due to a port conflict, change the default port. Go to Config → Apache (httpd.conf) and look for the Listen 80 directive. Change it to another port, like Listen 8080.
  3. Restart Services: After making changes, restart both Apache and MySQL from the control panel.

Port Conflicts with Apache

A common issue with XAMPP is a port conflict, especially if another application (like Skype or IIS) is using port 80, which is the default port for Apache. 

Here’s how to resolve it:

  1. Open XAMPP Control Panel and stop Apache.
  2. Change Apache’s Port:
    • Click Config → Apache (httpd.conf).
    • Search for the line Listen 80 and change it to Listen 8080 (or another available port).
    • In the same file, find ServerName localhost:80 and change it to ServerName localhost:8080.
  3. Save the file, restart Apache, and try accessing your project using http://localhost:8080/MyFirstPHPProject/index.php.

Resolving PHP Errors

You might encounter PHP errors while working with your files. These can be syntax errors, runtime errors, or logical issues. Here’s how to tackle them:

  • Common PHP Errors: Missing semicolons, incorrect variable names, or wrong function calls are some of the most common PHP errors.
  • How to Debug:

1. Check the XAMPP Logs: Click Logs in the XAMPP control panel to check for any PHP error messages.

2. Enable Error Reporting: To see all errors clearly, add this line to the top of your PHP script:

error_reporting(E_ALL);
ini_set('display_errors', 1);

3. Fix the Errors: The error messages should give you clues as to what went wrong. Follow the suggestions and re-test your script.

Now that you’re equipped to troubleshoot and resolve common issues in XAMPP, you can focus on building and running PHP projects smoothly. Let’s explore why using PHP with XAMPP is a winning combination for developers.

Advantages and Disadvantages of Using PHP and XAMPP

PHP is widely used because it’s an open-source, platform-independent scripting language that’s great for building dynamic websites. XAMPP makes it easy to set up a local server environment to run PHP projects, providing a complete package that includes everything you need to run PHP code.

Below is a table summarizing the advantages, disadvantages, and workarounds for using PHP and XAMPP.

Advantages

Disadvantages

Workaround

Open-source and Free: PHP is open-source and free to use, making it an accessible choice for developers worldwide. Security Issues: Running XAMPP on your local machine can expose your computer to potential security risks, especially if misconfigured. Secure Configuration: Always secure your XAMPP installation by disabling unnecessary services and using strong passwords.
Cross-platform Compatibility: XAMPP works across multiple operating systems (Windows, macOS, Linux), making it versatile for various environments. Limited for Production Use: XAMPP is meant for local development, not for live production environments. Use Production Servers for Deployment: Once you're ready to deploy, migrate your code to a more secure and robust server environment (e.g., Apache or Nginx).
Quick Setup for Local Development: With XAMPP, you can quickly set up a local environment to run PHP code without needing to configure servers manually. Resource-Heavy: XAMPP can slow down your system, but you can reduce usage by stopping unneeded services in the control panel. Optimize Services: Only start the services you need (e.g., disable MySQL if you don’t need a database).
Ideal for Learning and Testing: XAMPP offers a simple way to test PHP files locally before deployment, which is great for learning PHP or building small projects. Port Conflicts: XAMPP uses default ports (80 for Apache, 3306 for MySQL) that can conflict with other software on your machine. Change Default Ports: If you experience port conflicts, change the default port numbers in the XAMPP control panel (e.g., change Apache’s port to 8080).
All-in-one Package: With XAMPP, you get Apache (web server), MySQL (database), and PHP (scripting language) bundled together, making it easy to run PHP code in XAMPP. Limited Scalability: XAMPP is not designed to handle large-scale applications like a live server environment would. Use Lightweight Solutions for Production: For larger-scale projects, consider deploying to a more robust server setup.

PHP and XAMPP are a powerful combo for web development. XAMPP provides everything you need to develop and test PHP applications locally. 

Start creating your own PHP projects, experiment with different features, and test your code locally with XAMPP. The more you practice, the better you’ll get at debugging, testing, and refining your PHP applications. 

Conclusion

In conclusion, learning how to use XAMPP for PHP development is essential for efficiently building and testing dynamic web applications locally. By familiarizing yourself with the setup process, running PHP files, and troubleshooting issues, you'll improve your workflow and problem-solving abilities. Keep experimenting with different PHP projects, test your code regularly, and refine your skills to become more efficient in your development process. 

If you want to deepen your understanding of PHP development or explore other areas in the tech field, upGrad’s career counseling services can guide you in choosing the right path. Visit your nearest upGrad center today for in-person guidance and take the next step in advancing your career!

References: 

https://stitcher.io/blog/php-version-stats-january-2025
http://localhost/MyFirstPHPProject/index.php

 

Frequently Asked Questions (FAQs)

1. How do I configure XAMPP to run PHP files on a custom port?

2. Can I run PHP without MySQL using XAMPP?

3. What is the best way to test PHP code locally with XAMPP?

4. How do I enable error reporting in PHP while using XAMPP?

5. What should I do if Apache doesn’t start in XAMPP?

6. How do I secure XAMPP for production use?

7. How do I update PHP in XAMPP?

8. What is the significance of the htdocs folder in XAMPP for PHP?

9. How do I handle multiple PHP projects in XAMPP?

10. How can I use XAMPP for PHP development across multiple platforms?

11. How do I connect PHP to a MySQL database in XAMPP?

Mukesh Kumar

163 articles published

Get Free Consultation

+91

By submitting, I accept the T&C and
Privacy Policy

India’s #1 Tech University

Executive PG Certification in AI-Powered Full Stack Development

77%

seats filled

View Program

Top Resources

Recommended Programs

upGrad

AWS | upGrad KnowledgeHut

AWS Certified Solutions Architect - Associate Training (SAA-C03)

69 Cloud Lab Simulations

Certification

32-Hr Training by Dustin Brimberry

upGrad KnowledgeHut

upGrad KnowledgeHut

Angular Training

Hone Skills with Live Projects

Certification

13+ Hrs Instructor-Led Sessions

upGrad

upGrad KnowledgeHut

AI-Driven Full-Stack Development

Job-Linked Program

Bootcamp

36 Weeks