How to run PHP Program | CODE | File | Script → on Your Computer?

Summary: Easiest way to install & run PHP Program | CODE | File | Script on Your Own Computer → Run PHP using XAMPP

How to run PHP Program in XAMPP

To begin Programming in PHP for Web Development, the very first question you'll face is:

How to install PHP and run PHP Program (any CODE written in .php file) on your own Computer? What's the easiest way?

A "Program" is the result of many lines of "CODE" & since PHP is a Scripting Language, CODE written in PHP files are sometimes called PHP "Scripts". These are all interchangable words, so don't get confused by these terms.

Few short notes before you install & run PHP:

The easiest way to install PHP & run PHP Program or any CODE written in *.php file on your own computer is using a software bundle like XAMPP.

There are other similar software bundles like MAMP, WAMP etc. that can install and run PHP files, but:

XAMPP works well in all the major Operating Systems like Windows, Mac and Linux the same way. So XAMPP is easier to start with.

To run PHP program for web development, you also need a web server like Apache, and a database server like MySQL or MariaDB. You may install these separately on your own computer, however, it is often difficult for beginners to install these tools individually and combine them to run PHP files. XAMPP does all these for you with just a few clicks.

Note: If Apache, MySQL or PHP is already installed on your computer, then before installing XAMPP, you may have to uninstall the existing Apache, MySQL and PHP installations, especially if they were installed as service. It's possible to keep multiple installations, but that's for more advanced users.

OK, enough with the initial description, now follow the steps below to run your first PHP CODE:

How to Install PHP:

  1. Download XAMPP from here  and then double click the downloaded installer file to begin XAMPP installation.
  2. Just follow the instructions from the installer and you'll be done installing PHP within a few clicks.

8 Simple Steps to Run PHP Program or CODE written in .php files:

  1. After the installation of XAMPP is successful, open any CODE Editor.

    Install a new CODE Editor if you don't have one installed on your computer. My Favorite is  Visual Studio CODE (for Windows, Mac & Linux).

    Examples of other popular light weight CODE Editors are: Sublime Text (for Windows, Mac & Linux), Notepad++ (for Windows) etc.

  2. Write the following PHP Program / CODE in the CODE Editor of your choice:
    <?PHP
    echo 'I know how to run a PHP Program in XAMPP! <br />';
    ?>
    This is test.php File.
  3. Save the file in XAMPP Installation Directory \ Web Root Directory
    Note-1: Default XAMPP Installation Directory in Windows is C:\xampp
    Note-2: Default Web Root Directory in XAMPP is htdocs. All your php files will have to be in this htdocs folder.

    That means, for a typical installation of XAMPP on Windows, you will have to save the PHP file in C:\xampp\htdocs folder.

  4. When you save the file, name it test.php
    (test.php is just an example, any valid file name with .php in the end will work).

    Note: when saving this file, make sure it has no .txt extension at the end. Some Editors place .txt at the end of the file name, so it becomes test.php.txt instead of test.php. To avoid this, when you save the file using any Editor, place double quote around the file name: e.g. "test.php"

  5. Then, go to XAMPP installation folder (typically, C:\xampp) and run xampp-control.exe by double clicking it.

    Note: if your Operating System hides common file extensions, then you'll see xampp-control, instead of xampp-control.exe

  6. In the xampp-control window, click the start button beside Apache. Later, if you need other options like MySQL, you'll also have to start MySQL by clicking the start button beside MySQL in the XAMPP control Panel.
  7. Now open your favorite  web browser (Firefox, Google Chrome etc.) and enter this LINK: http://localhost/test.php in the address bar of your web browser.

    This Link must match the file name you've used to save the PHP CODE in step-6 above. So instead of test.php, if you've saved the PHP file as example.php, then your Link should be:

    http://localhost/example.php

  8. If your PHP installation and the test PHP file you've just written is working properly, then you should see the following two lines on your browser:

    I know how to run a PHP Program in XAMPP!
    This is test.php File.

    Congratulation! ✌️

    Now you know how to run PHP CODE or any *.php file on your own computer using XAMPP. May be it is time to learn some proper web development with PHP!

PHP Learning Resources:

Best way to learn a programming language is by doing. While you can learn from manuals, books, online articles etc. it's often easier & cost effective to learn a new programming language from online video courses. Here's some PHP learning resources I like:

  1. PHP Online Course → PHP for Beginnersicon

    This is an updated & beginner friendly online video course on web development with PHP. With this course you'll learn PHP & how to create dynamic PHP websites along with a content management system project.

  2. Another Online PHP Course → Learn PHP Programming From Scratch

    Another decent paid video course on web development with PHP.

  3. Reference Manual → Official PHP Manual

    This is the most comprehensive guide written on PHP, read the manual and go through the comments within the manual if you can. It's a bit difficult and time consuming compared to video courses, but if you want to master PHP, you'll need to at least keep it for reference.

Happy Coding 😊

Troubleshoot PHP installation

Uh, Houston, we've had a problem!

If the above instructions worked for you & you can run your PHP Scripts just fine, then you don't need to read this section, however, if you didn't get the expected result from the above instructions, don't panic, we've all been there. To get you started with what's going on:

If PHP is NOT working but the web server Apache is working, in that case only the following one line result will appear:

This is test.php File.

If you are getting an error instead, then Google for that particular error text and with any luck, you'll receive tons of results out there.

Also, check out the comments here, other people may have already faced similar issues, and your solution may just be in the comments as well.

117 thoughts on “How to run PHP Program | CODE | File | Script → on Your Computer?”

    1. This means you have an error within your PHP CODE. Write proper PHP syntax, don't even misplace a semicolon ;, then your CODE will run without any error.

  1. thanks! it was everything I was looking for to understand what the heck with PHP. (Installation, small example and how to execute it).

  2. I've been trying since yesterday and I still get the 404 error. Apache is running on port 81. The test file is in htdocs and I put "test.php" when I saved the file, but still. 😔 Help guys.
    The code inside the file is the same as the one you said we should try.

  3. Hi, Thanks for this article, but i am having issue with Apache. Skype / Outook will be always running in my system. Please see what wrong is happening here.

    6:34:56 PM [Apache] Attempting to start Apache app...
    6:34:57 PM [Apache] Status change detected: running
    6:34:57 PM [Apache] Status change detected: stopped
    6:34:57 PM [Apache] Error: Apache shutdown unexpectedly.
    6:34:57 PM [Apache] This may be due to a blocked port, missing dependencies,
    6:34:57 PM [Apache] improper privileges, a crash, or a shutdown by another method.
    6:34:57 PM [Apache] Press the Logs button to view error logs and check
    6:34:57 PM [Apache] the Windows Event Viewer for more clues
    6:34:57 PM [Apache] If you need more help, copy and post this
    6:34:57 PM [Apache] entire log window on the forums
    6:35:01 PM [Apache] Problem detected!
    6:35:01 PM [Apache] Port 80 in use by "Unable to open process" with PID 4!
    6:35:01 PM [Apache] Apache WILL NOT start without the configured ports free!
    6:35:01 PM [Apache] You need to uninstall/disable/reconfigure the blocking application
    6:35:01 PM [Apache] or reconfigure Apache and the Control Panel to listen on a different port

  4. Object not found!

    The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

    If you think this is a server error, please contact the webmaster.
    Error 404
    localhost
    Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/7.0.13
    this error is showing

    1. I Need some details before I can answer:

      1. What is the name of your PHP file/script?
      2. Where did you place this file (full path of the file)?
      3. Copy the exact PHP CODE
      4. What did you enter in your browser address bar?

  5. it shows an error whenever click on start:
    7:55:32 PM [mysql] Status change detected: stopped
    7:55:32 PM [mysql] Error: MySQL shutdown unexpectedly.
    7:55:32 PM [mysql] This may be due to a blocked port, missing dependencies,
    7:55:32 PM [mysql] improper privileges, a crash, or a shutdown by another method.
    7:55:32 PM [mysql] Press the Logs button to view error logs and check
    7:55:32 PM [mysql] the Windows Event Viewer for more clues
    7:55:32 PM [mysql] If you need more help, copy and post this
    7:55:32 PM [mysql] entire log window on the forums

    what should i do??

    1. Exit XAMPP, then:
      1. Go to MySQL data directory (default in Windows is: C:\xampp\mysql\data)
      2. Rename the ibdata1 file to something else (for example: ibdata1.backup)
      3. Restart XAMPP control & try to start MySQL again.
      Should work.

  6. It works well!!! I started learning PHP today and have been struggling to run the code in a browser until I came to this page.
    Thanks a million.

  7. i have some php file ready to run some kind of software to send emails. how can i place file on proper folder to run this software. urgent help needed.

    1. It shows this error message when I hit start.

      "

      PM 12:54:21 [Apache] Attempting to start Apache app...
      PM 12:54:22 [Apache] Status change detected: running
      PM 12:54:25 [Apache] Status change detected: stopped
      PM 12:54:25 [Apache] Error: Apache shutdown unexpectedly.
      PM 12:54:25 [Apache] This may be due to a blocked port, missing dependencies,
      PM 12:54:25 [Apache] improper privileges, a crash, or a shutdown by another method.
      PM 12:54:25 [Apache] Press the Logs button to view error logs and check
      PM 12:54:25 [Apache] the Windows Event Viewer for more clues
      PM 12:54:25 [Apache] If you need more help, copy and post this
      PM 12:54:25 [Apache] entire log window on the forums

      "

      What to do?

      1. you can try using another port cause may be the port that you are currently using is already in use.

        1. Either Apache is already installed on your PC or some other app is using port 80. Usually Skype does that. Stop any such app, restart your PC to be sure and try again.

Leave a Reply

Your email address will not be published. Required fields are marked *