How to Run a PHP File in XAMPP
If you’re just getting into web development or testing, you’ve most likely been recommended to get XAMPP. It is a free, stable, and regularly updated server solution. In case you’re programming in PHP, you’re in luck, as running PHP files in XAMPP is a piece of cake. Keep reading to find out how to do it.
What Is XAMPP?
XAMPP is a cross-platform and open-source web server package. It is completely free to download and use and is developed and updated by Apache Friends. The main components include the Apache HTTP Server and the MariaDB database. Also included are the interpreters that can read Perl and PHP scripts.
XAMPP is compatible with Linux, Windows, and Mac operating systems, making it a super-versatile server solution. It also supports LAMP and WAMP stacks.
XAMPP is an acronym, with the letters standing for the following:
- X is for cross-platform
- A is for the Apache server
- M is for the MariaDB database (it used to stand for MySQL)
- P is for PHP
- P is for PERL
Open a PHP File in XAMPP
Opening and running PHP files in XAMPP is easy and simple. All you need is a working copy of XAMPP and a PHP file. Let’s see how you can do this.
First, let’s get XAMPP. You’ll need to get an appropriate version for your operating system from the Apache Friends’ official site.
Launch your favorite web browser and go to https://www.apachefriends.org/index.html. When the Home page of the site opens, you should click on the latest version of the program for your operating system. At the time of this writing, PHP 7.3.7 is the latest one for all three major OS platforms. Click on the one you need to download it.
In case you want an older version, click on the “Click here for other versions” link within the green Download field.
Next, you should install XAMPP on your computer. The most common practice is to install it on the C drive, though some users opt to install it on D or other non-system partitions.
To install XAMPP, double-click on the installer file you downloaded. Click on the Run button to start the installation. Follow the installation wizard to complete the installation. In the Select Components section, you can opt to leave some components out if you’re sure you won’t need them. However, it is recommended to keep everything as is and install the full version.
The default location is C:\xampp. You can change it if you want to, but it is recommended not to.
In this step, you’ll create the project folder where you’ll save the PHP file you’d like to run in XAMPP. To do that, go to your XAMPP folder. If you haven’t altered installation options, your copy of XAMPP is installed on the C drive.
Once you get to the XAMPP’s main folder, go to the htdocs folder. There, create a new folder and name it after the file you’ll copy into it. Alternatively, you can name it “NewProject,” “Project 1,” or give it any other name.
If you already have a file that you want to open with XAMPP, browse for it. Once you’ve located it, copy and paste it into the folder you created in the previous step. You can also cut and paste the file.
If you don’t have the file and you want to create your first PHP file to test XAMPP, here’s what to do. Go to the folder you created in the previous step and create a new text file in it. Name the file and change its extension from txt to php. Before we start, here is a word of warning: don’t enter the full stops at the end of the commands, as they are only there for grammatical purposes.
Open the file with a text editor, both Notepad and Notepad ++ will be good. Start your HTML document with <html>. In the next row, open the header with <head><h1>Title</h1>. Close the header in the next row with </head>.
Next, open the body container with <body> in the next row. Press Enter a few times and press TAB for indent. Type in <?php. Press Enter once or twice and write the following command print “text”. Between the quotations, you should write what you want to see when you open the file with XAMPP.
Press Enter once more and type ?>. This will end the PHP section. In the next row, you should type </body>. In the row below that one, you should type </html>. Press Ctrl and S keys on your keyboard to save the changes to the file. Your file might look like this at the end:
Now, let’s open the file with XAMPP. Here’s how to do it:
- Launch XAMPP. You can open it via the status bar icon or search for it in the Start menu if you’re on a Windows computer.
- Once the app opens, you will see its Control Panel. Click on the Start button in the Apache row. This will launch the Apache server. The Apache label in the Module column will turn green and you’ll see a message in the log at the bottom of the Control Panel informing you that the server is running.
- Now, open your browser and type “localhost” into the address bar (omit the quotations).
- You should see the “localhost – Welcome to XAMPP” option right below the address bar. Click on it.
- You will be redirected to localhost/dashboard. Replace the dashboard part with the name of the folder where your PHP file is located. In our case it looks like this: localhost/NewProject.
- You will see the list of files in the said directory. Click on your PHP file.
If everything’s OK, you should see your PHP file open in your browser.
XAMPP to the Max
As a free and open-source solution, XAMPP is a common option for web developers and testers running on a tight budget. What’s more, many professionals use it, too. The fact that it’s compatible with Linux, Windows, and Mac makes it even more popular.
Do you use XAMPP? Have you ever had problems running PHP files and how did you solve them? Share your thoughts and experiences with XAMPP in the comments section below.