How to run PHP Program, CODE or File on Your own Computer?

Summary: Easiest way to install & run PHP on Your Own Computer: Run PHP using XAMPP.

How to run PHP Program in XAMPP

To begin Programming in PHP or to check an existing PHP site or script on your own computer, the very first question you'll face is:

How to run PHP CODE or any File ending with .php extension on My Own Computer? What's the easiest way?

A PHP Program is the result of many lines of PHP CODE & often includes many PHP files. Also, since PHP is a Scripting Language, CODE written in PHP files are often called PHP Scripts. These are all interchangeable words. So don't get confused by these terms.

A Short Introduction:

The easiest way to install & run PHP on your own computer is using a software like XAMPP.

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

XAMPP works well in all the major Operating Systems like Windows, Mac & Linux the same way. So XAMPP is easier for beginners.

PHP can be used for different purposes, however, the most popular use of PHP is as the server side programming language for web development. Naturally, to run PHP program for web development, you also need a web server like Apache Web Server, 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 programs. XAMPP does all these for you with just a few clicks.

OK, enough with the introduction, 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.

How to run PHP Program:

  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 (works on Windows, Mac & Linux).

    Examples of other popular CODE Editors are: Sublime Text (works on Windows, Mac & Linux), Notepad++ (works on Windows only) etc.

  2. Write the following PHP 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 files 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

    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 database, you'll also have to start MySQL by clicking the start button beside MySQL in xampp-control.

  7. Now open your favorite web browser like Firefox, Google Chrome etc. (on the same computer where you've just installed PHP) & enter this LINK: http://localhost/test.php in the browser's address bar (or simply click http://localhost/test.php if you are reading this post on the same computer where you've just installed XAMPP & running PHP).

    Two things to note here:

    1. On the web, localhost means your own computer.
    2. The test 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 file ending with *.php extension on your own computer using XAMPP. Perhaps it is time to learn some proper web development with PHP!

PHP Learning Resources:

The best way to learn programming is by doing a lot of practice. I recommend the following resources for beginners:

  • PHP for Beginners:icon This is a well organized & beginner friendly online PHP learning course. While you can learn PHP from articles & tutorials here & there, they often lack completeness. That's why it's far easier & more effective to begin the learning process from a course like this.
  • PHP Manual: Bookmark this official PHP manual. Reading the manual is not the easiest way to begin your PHP learning  process, but you'll always need this as a 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, then only the following one line result will appear:

This is test.php File.

If you are getting an error instead, then  check out the comments here or Google for that particular error text & with any luck, you'll receive tons of results.

Note: If Apache, MySQL or PHP was 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.

115 thoughts on “How to run PHP Program, CODE or File on Your own Computer?”

  1. Its showing error...
    [Mon Jul 18 16:28:52.020621 2016] [ssl:warn] [pid 6128:tid 388] AH01909: http://www.example.com:443:0 server certificate does NOT include an ID which matches the server name
    [Mon Jul 18 16:28:52.476219 2016] [ssl:warn] [pid 6128:tid 388] AH01909: http://www.example.com:443:0 server certificate does NOT include an ID which matches the server name
    [Mon Jul 18 16:28:52.742248 2016] [mpm_winnt:notice] [pid 6128:tid 388] AH00455: Apache/2.4.18 (Win32) OpenSSL/1.0.2e PHP/7.0.8 configured -- resuming normal operations
    [Mon Jul 18 16:28:52.742248 2016] [mpm_winnt:notice] [pid 6128:tid 388] AH00456: Apache Lounge VC14 Server built: Dec 9 2015 10:17:39
    [Mon Jul 18 16:28:52.742248 2016] [core:notice] [pid 6128:tid 388] AH00094: Command line: 'c:\\xampp\\apache\\bin\\httpd.exe -d C:/xampp/apache'
    [Mon Jul 18 16:28:52.757857 2016] [mpm_winnt:notice] [pid 6128:tid 388] AH00418: Parent: Created child process 3848
    [Mon Jul 18 16:28:53.554741 2016] [ssl:warn] [pid 3848:tid 408] AH01909: http://www.example.com:443:0 server certificate does NOT include an ID which matches the server name
    [Mon Jul 18 16:28:53.804743 2016] [ssl:warn] [pid 3848:tid 408] AH01909: http://www.example.com:443:0 server certificate does NOT include an ID which matches the server name
    [Mon Jul 18 16:28:53.867263 2016] [mpm_winnt:notice] [pid 3848:tid 408] AH00354: Child: Starting 150 worker threads.

  2. i am getting the 404 file not foung error because the port is not set for apache server i need to set the port how should i do it

    1. Web server default port is 80 and it's normally set automatically by Apache, unless you are using Skype (or some other software) and that is using port 80 already. In that case you'll have to disable port 80 from Skype settings.

      However, 404 is normally not shown for port, it's shown if the file you are trying to access from browser doesn't actually exist in web server root directory.

      So make sure the file exist and there is no typing mistake in the link you are trying to access in the browser.

  3. Hi Mr. fayaz

    I try the xampp this is my first time. And i understand the process about xampp but i am doubt about the result when i run the first sample in the http://localhost/test.php and the result is this.

    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

    127.0.0.1
    Apache/2.4.17 (Win32) OpenSSL/1.0.2d PHP/5.6.21

    What is this?
    Error !
    what do you mean by two lines mr. fayaz.

    thanks looking forward for your response.

  4. PM 2:55:12 [Apache] Error: Apache shutdown unexpectedly.
    PM 2:55:12 [Apache] This may be due to a blocked port, missing dependencies,
    PM 2:55:12 [Apache] improper privileges, a crash, or a shutdown by another method.
    PM 2:55:12 [Apache] Press the Logs button to view error logs and check
    PM 2:55:12 [Apache] the Windows Event Viewer for more clues
    PM 2:55:12 [Apache] If you need more help, copy and post this
    PM 2:55:12 [Apache] entire log window on the forum

    this error is coming when i start appache

Leave a Reply

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