How to Run PHP Program in XAMPP?

Posted in /  

How to Run PHP Program in XAMPP?
Aashiyamittal

Aashiya Mittal
Last updated on December 26, 2024

    Despite the continuously changing landscape of programming with the emergence of newer programming languages like Go, Groovy, Rust, and Scala and the enhancement of already popular languages like Java and Python, PHP manages to hold on to its repute of being the most popular backend programming language .

    A PHP program can either run as a web server module or from the command-line interface. To run PHP code for the web, there is the need to install a web server like the Apache Web server along with a traditional relational DBMS such as MySQL or a modern DBMS like MariaDB.

    Although there are several web server solution stacks available for running PHP programs, the two immensely popular options are WAMP and XAMPP. Before exploring how to run a PHP program in XAMPP, first let’s have a brief introduction to XAMPP and how to install the web server solution stack.

    What is XAMPP? [Definition]

    XAMPP is a free and open-source web server solution stack developed by Apache Friends . It facilitates testing and deploying PHP and Perl applications by creating a local webserver. The software distribution server is among the most popular testing and deploying environments for the PHP language.

    XAMPP is a cross-platform solution stack, available for the macOS, Microsoft Windows, and Linux platforms. The name XAMPP is an acronym, where each alphabet, leave for X, represents one of the various technologies that the software distribution server solution stack comprises of:

    • X – Cross-platform
    • a – Apache HTTP Server
    • m – MariaDB database (previously, MySQL)
    • p – PHP interpreter
    • p – Perl interpreter

    Everything required to set up a web server for testing PHP programs is included in XAMPP; Apache HTTP Server as the server application, MariaDB as the DBMS, and a PHP interpreter for parsing and executing the PHP code. Web development leveraging the simple and lightweight XAMPP is beginner-friendly.

    Hence, even newcomers and students can easily get started with the free web server solution stack. Transitioning from a local test server using XAMPP to a live server using some other web server deployment is extremely easy. This is because most web server deployments use the same components as that used by XAMPP itself.

    How to Install and Setup XAMPP?

    One of the major highlights of XAMPP is its ease of installation. Documentation on XAMPP is available in the form of FAQs on the official website of Apache Friends. You can simply download and begin with XAMPP by visiting the official Apache Friends website. Installing and setting up XAMPP is quite simple and straightforward. Here’s how to install XAMPP on your system:

    • Step #01 – Go to https://www.apachefriends.org/download.html from your web browser. Here you will find a list of all the XAMPP versions available for macOS, Microsoft Windows, and Linux platforms. Once you’ve selected the version of the web server solution stack that you need to install, hit the accompanying Download button to start downloading the XAMPP installer.

    Note : If you don’t know which version to opt for, try choosing the recent one available.

    • Step #02 – Open the XAMPP installer and click on the Next button to go to the Select Components screen. Here, you can choose the various components that you can install along with XAMPP. Once done, click on the Next button to proceed.
    • Step #03 – You will now be on the Installation folder screen. Here, you can choose the directory where XAMPP will be installed. The default option will be C:\xampp (for Windows). You can change it if you need. Click Next to start installing XAMPP on your system.
    • Step #04 – Once the installation is complete you will be presented with a final screen. Hit the Finish button to complete the XAMPP installation.

    Note : The XAMPP download page also contains the various add-ons and themes available for XAMPP, which you can easily install on top of XAMPP.

    How to Run a PHP Program in XAMPP?

    Before starting with the process of running a PHP program in XAMPP, first, write the PHP code that you need to execute in a notepad and save the file with the name phpsamplefile.php. Note : If you don’t have any PHP code to test, add the following code to the file: <?php echo "Hello World!" ?> This will display Hello World! as the output.

    Step #01 – Go to the XAMPP Control Panel

    Launch XAMPP. You will start with the XAMPP Control Panel. Here, start the Apache HTTP Server and MariaDB Server.

    Step #02 – Move the PHP File to htdocs

    Copy/Cut the notepad file containing the PHP code to the htdocs folder present at C:/Program Files/XAMPP/htdocs.

    Note : htdocs is the default directory where the Apache HTTP Server looks for files to serve on your domain. You can change this location by pointing the Document Root to a folder of your choice.

    Step #03 – Go to XAMPP Dashboard

    Now, open a web browser on your system and go to http://localhost. This will take you to the XAMPP dashboard.

    Note : This step will only work when you have initiated the Apache and MariaDB servers in XAMPP.

    Step #04 – Execute the PHP Code!

    Now, it’s time to execute the PHP program. To do so, enter localhost/phpsamplefile.php in the address bar and hit enter. The executed PHP program will be displayed in the browser window.

    When to Choose XAMPP?

    Both XAMPP and WAMP are free-to-use web server solution stacks that can be used for running PHP and Perl programs locally. While WAMP only works on Windows-based platforms, XAMPP is a cross-platform web server solution stack that also provides support for macOS and Linux platforms aside from Microsoft Windows.

    The most important aspect that one needs to take care of while choosing between WAMP and XAMPP is security. While XAMPP is developed especially for local (development) projects, WAMP is built for providing secure web server deployment.

    Although XAMPP can also be used to host live websites, offering the same level of security in XAMPP as that offered by WAMP requires making a lot of adjustments to the web server solution stack. XAMPP is the best choice when working on hobby projects, while WAMP is preferred when working on production environment-oriented projects requiring a good level of security and have a system running Microsoft Windows.

    Provided the same configuration, WAMP offers better security than XAMPP. When, however, learning is the primary concern, XAMPP is the go-to option. This is because it is a beginner-friendly software distribution server solution stack.

    Conclusion

    By now, you will be able to execute a PHP program in XAMPP. You can always test your PHP programs locally using XAMPP before advancing with them. Found any misinformation/outdated information in the article? Help us mend it by mentioning the same in the comments section below. Suggestions regarding making the article better are always welcome.

    People are also reading:

    Leave a Comment on this Post

    0 Comments