How to Run a PHP File using XAMPP

How to Run a PHP File using XAMPP

A PHP code runs on a web server. But how can a web developer working from home set up a server to run the code, check for bugs, and see how it behaves before going live? That’s where tools like XAMPP come in handy. XAMPP provides a simple and effective way to create a local server environment on your machine. To start running PHP code locally, you just need three things: a code editor, an installed version of PHP, and XAMPP. In this comprehensive guide, we will walk you through every step, from installing XAMPP to executing your first PHP script. Let us understand the complete process.

Table of Contents:

What is PHP?

PHP stands for Hypertext Preprocessor. Before, it used to stand for Personal Home Page.  It is an open-source, server-side scripting language that is widely used for developing dynamic web pages. PHP acts like the brain of your website, determining how data is processed and which content is served to users based on their requests. It is used to make webpages that are both responsive and interactive with the database.

PHP 1

Advantages of PHP 

  • PHP is simple to use, and its syntax is easier to understand. The syntax usually tells you what it does. 
  • PHP allows you to make changes even after the website has been launched. This makes PHP flexible.
  • PHP helps you collect data from a form on the website. This functionality is widely used.
  • PHP has good security. It does not outsource the data it collects.

What is XAMPP?

XAMPP is a free and open-source package that bundles together key components of a web development stack and allows you to run a web server on your computer. XAMPP stands for Cross-platform, Apache, MySQL, PHP, and Perl, which are the various technologies included in the XAMPP package. 

With XAMPP, developers can simulate a production-like server environment on their local machine. It is available on Windows, macOS, and Linux.

XAMPP

Why Do You Need XAMPP?

  1. By bundling together Apache, MySQL/MariaDB, PHP, and Perl, XAMPP makes PHP development a much simpler process.
  2. With XAMPP, users can run PHP files locally, so there’s no need for internet connectivity or live hosting to check PHP during development.
  3. XAMPP has a user-friendly interface and bundled usage of phpMyAdmin, to manage databases with ease.
  4. XAMPP provides a safe environment to test things and prototype projects without putting the live servers at risk.

How to Install XAMPP?

Follow the steps below to set up a local server environment for PHP development.

Step 1: Download XAMPP Installer

Visit the official Apache Friends website. Select the download button of your operating system and select the most recent version unless you purposefully want to install an older version.

Step 1: Download XAMPP Installer - 1

For this demo, we will install it on Windows OS. Once you click on the ‘XAMPP for Windows’ button, the download starts.

Step 1: Download XAMPP Installer - 2

Step 2: Run the XAMPP Installer

Now we will run the installer that we downloaded earlier. 

  • Since we downloaded for Windows, it will be downloaded in .exe format. Locate the downloaded .exe file.
  • Double-click the installer to start the setup wizard.
  • If prompted by User Account Control, click Yes to proceed.
Step 2  Run the XAMPP Installer

Step 3: XAMPP Setup Wizard

After double-clicking the XAMPP setup wizard will launch.

Step 3 XAMPP Setup Wizard - 1

Click Next to continue. You’ll see a list of components like Apache, MySQL, FileZilla, phpMyAdmin, etc. We recommend keeping the default options selected (especially Apache, MySQL, PHP, and phpMyAdmin).

Step 3 XAMPP Setup Wizard - 2

Step 4: Choose Installation Folder

  • Choose the folder where you want to install XAMPP.
  • It is recommended to install it in the root directory(C:) as a separate folder and not inside “Program Files”, to avoid permission issues.
  • Click Next to continue.
Step 4: Choose Installation Folder

Step 5: Begin Installation

  • Once you have added all the configuration settings, language, you will get a ready-to-install window box. 
Step 5: Begin Installation - 1
  • Click on Next to begin installation.
Step 5 Begin Installation - 2
  • This may take a few minutes. Once the installation is complete, you will see a completion message.

Step 6: Launch XAMPP Control Panel

  • Check the box that says “Do you want to start the Control Panel now?”
  • Click Finish to exit the wizard and launch the XAMPP Control Panel.
Step 6: Launch XAMPP Control Panel

Step 7: Start Apache and MySQL

  • The control panel will launch.
  • In the XAMPP Control Panel, click the Start buttons next to Apache and MySQL.
  • The module names will turn green when they are running successfully
Step 7: Start Apache and MySQL

Step 8: Verify the Installation

  • Open your web browser.
  • Go to http://localhost
  • If XAMPP is installed correctly, you’ll see the XAMPP Welcome Page like this.
Step 8: Verify the Installation

Common Mistakes When Installing XAMPP

Installing XAMPP may be a simple process, but as a beginner who does not have knowledge of what goes on behind the scenes, you might make mistakes. This section will help you avoid some of the common mistakes. Don’t panic. These mistakes are common and easily fixable.

1. Installing XAMPP in the “Program Files” Directory

Choosing C:Program Files or C:Program Files (x86) as the installation folder will raise errors like “not an empty folder”. System directories like ‘Program Files’ have restricted permissions, which can prevent XAMPP components from writing files or logs properly. Always install XAMPP in a root-level, empty directory like C:xampp or C:Intellipaat.

2. Not Running XAMPP as Administrator

Beginners often launch the XAMPP Control Panel without administrative privileges. Without administrative privileges, XAMPP may be unable to start services, bind to ports, or access system directories. Therefore, whenever you are running the XAMPP Control panel, always right-click XAMPP Control Panel (xampp-control.exe) and “Run as administrator”.

3. Forgetting to Start Apache and MySQL Manually

Apache and MySQL do not turn on automatically. You will have to start these services manually. Projects won’t load on localhost if services are not running. Manually start Apache and MySQL via the XAMPP Control Panel every time, or you can set them to auto-start in the config.

Get 100% Hike!

Master Most in Demand Skills Now!

How to Run PHP Code Using XAMPP

To run a PHP file using XAMPP, you just have to follow a few simple steps.

Step 1: Open XAMPP Control Panel

Open xampp-control.exe. Remember to run as an administrator. Right-click and select “Run as administrator” to avoid permission issues.

How to Run PHP Code Using XAMPP - 1

Step 2: Start the Apache Module

Start the Apache module. To do that, in the XAMPP Control Panel, click “Start” next to Apache. Wait until the status bar turns green, indicating it’s running.

How to Run PHP Code Using XAMPP - 2

Step 3: Create Your PHP File in the htdocs Folder

Create your PHP file inside the htdocs folder. Make sure to place all your PHP files inside the htdocs folder to run them correctly.

How to Run PHP Code Using XAMPP - 3

Step 4: Run the PHP File in Your Browser Using localhost

Run the file in the browser using localhost by typing http://localhost/file_name.php.
If the file is inside a subfolder, use http://localhost/subfolder/file_name.php.

How to Run PHP Code Using XAMPP - 4

Now we will discuss htdocs folder, accessing the PHP file using localhost, and testing the PHP Setup Using phpinfo() in detail below. 

Understanding the htdocs Folder in XAMPP

When working with XAMPP to run PHP files locally, one of the most important directories you must learn about is the htdocs folder. This folder is responsible for file handling by your local web server and serving them to the browser.

The htdocs folder is the web root directory for the Apache web server bundled with XAMPP. Any files you place in this folder can be accessed through your local server via localhost.

When you visit http://localhost/, the files from this folder are displayed. 

The HTML, CSS, JavaScript, and image files are stored here. This folder is used to simulate how a live web server would deliver files to the browser.

Tip: Avoid using spaces or special characters in file/folder names, as they may not resolve correctly in the browser.

Testing the PHP Setup Using phpinfo()

Once XAMPP is successfully installed, it is time to verify whether PHP is correctly set up and running on your local server. Verifying the installation of PHP allows you to confirm your development environment is fully working and allows you to run PHP scripts with no complications.

Step 1: Launch XAMPP Control Panel

  • You can start by opening your XAMPP Control Panel by clicking on the xampp-control.exe file. This file would be located in the directory where you installed your XAMPP. Make sure you run it as Administrator to make sure you do not have any permission issues.
Step 1 Launch XAMPP Control Panel - 1
  • Start the Apache module by clicking the Start button next to it. Once it turns green, your Apache server is running, and PHP is now ready to interpret your files.
Step 1 Launch XAMPP Control Panel - 2

Step 2: Navigate to the htdocs Folder

  • Open your code editor. Here we will use VSCode. Open the htdocs folder in VS Code editor.
Step 2 Navigate to the htdocs Folder - 1
  • Create a test.php script that outputs PHP configuration details.
Step 2 Navigate to the htdocs Folder - 2

Step 3: Run the PHP File in a Browser

  • Now it is time to access the PHP script through your browser to verify PHP execution. 
  • Open your browser. In the search bar of the browser, type http://localhost/test.php and press Enter. This step is referred to as ‘accessing the PHP file using localhost’.
  • If your PHP installation is correct, you will see a comprehensive page titled “PHP Version…” along with detailed PHP configuration, loaded modules, and environment settings as shown below in the image.
Step 3: Run the PHP File in a Browser

Common Mistakes When Running a File Using XAMPP

Below, we highlight the most common mistakes when running a file using XAMPP and how to avoid them.

1. Saving Files Outside the htdocs Directory

XAMPP only recognizes files stored inside its htdocs directory. Saving your PHP file in any other folder and trying to access it via localhost will not work.

2. Incorrect URL or File Path in Browser

This is a common beginner mistake. Beginners tend to type the wrong file name or forget to include some subdirectories in the browser URL. Ensure that your URL is correct and has the correct relative file path, if applicable. For example, if your PHP file is in a subfolder named project1, access it using http://localhost/project1/filename.php.

3. Using the Wrong File Extension

Files must end in a .php extension. Naming your file index.txt or index.html will not run the PHP code inside the file. Always save your file as a .php file.

4. Apache Port Conflicts (Usually with Skype or IIS)

If Apache fails to start, it might be due to port conflicts with other apps like Skype or IIS. Edit the httpd.conf file and change Listen 80 to Listen 8080, then restart Apache. Access your site using http://localhost:8080/filename.php.

5. Not Restarting Apache After Configuration Changes

If you make changes to Apache configuration files, such as httpd.conf, php.ini, you have to remember to restart Apache, or else they won’t apply. Click “Stop” and then “Start” next to Apache in the XAMPP Control Panel after any config changes.

6. Using a Reserved File Name

Don’t use reserved filenames like con.php, nul.php, or aux.php, as they may cause unintended behavior to happen. Use a standard naming convention like index.php, dashboard.php, test.php, etc.

Conclusion

Setting up a local PHP development environment using XAMPP is a necessity for anyone looking to practice their web development skills and make a career in the same. With XAMPP, you can run PHP files on your local server, making it easy to test, debug, and build your applications. It will save you the wait time of needing a live web host to allow you access. After going through this blog, you will now have an understanding of how to run a PHP file using XAMPP. You can leverage this skill and start working on a simple contact form or even make a full-scale web application. XAMPP offers a hassle-free way to execute PHP code locally and experiment freely. So fire up XAMPP, run your PHP scripts on localhost, and start developing now!

To learn more about PHP, how to become a PHP developer, and gain hands-on experience. Also, prepare for job interviews with PHP interview questions prepared by industry experts.

How to Run a PHP File using XAMPP – FAQs

Q1. How to run the PHP file in XAMPP?

You can run a PHP file in XAMPP by saving it inside the htdocs directory, starting the Apache icon in the XAMPP control panel, and accessing http://localhost/yourfile.php in your web browser.

Q2. How to run a .PHP file?

You can run a .php file using a local server like XAMPP. Save the file in the htdocs folder, start the Apache button in the control panel, and then access the file at http://localhost/filename.php in your web browser.

Q3. How to run a command in XAMPP?

You can run the commands for XAMPP using the terminal within XAMPP, or using Windows Command Prompt. Go to the PHP or MySQL directory and run the commands, for example, php yourfile.php for PHP scripts.

Q4. How to run a PHP file in Chrome?

You can open Chrome and enter http://localhost/yourfile.php after placing the PHP file in the XAMPP htdocs folder and starting Apache. Chrome will display the output of your PHP script.

Q5. How to run a PHP file using localhost?

You can run it by saving the file in the htdocs folder, launching XAMPP’s Apache server, and visiting http://localhost/filename.php in your browser.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner