How To Test PHP Code On Localhost

Can I run PHP Code on my computer? Can I create and test a static PHP page on my local server? How to test PHP on my computer?

The simple answer is yes.

There are many ways to test PHP, but one common and easiest way is to get a server solution called XAMPP. It is open-source software that creates a local server on your system and is all done to run your PHP files.

Most of the developers use XAMPP or similar server solution software for WordPress development and other platforms that work on PHP script.

Tools you will need to run PHP code

To run any PHP code on the local machine you need?

  1. Any PHP software that will make your computer a server. Here we will go with XAMPP.
  2. A Code Editor, even the default one will work. For demo purposes, we have used NotePad.
  3. Command prompt app: CMD/Terminal.

Follow the steps to test PHP file on the computer

Get ready with all your tools open.

As the XAMPP will make the PHP execute through the browser, you have to put the files under the XAMPP folder.

1. Create file and folder

  • Create a folder, like here we will have “demo” and “index.php” in it. Remember this must be under the “htdocs” folder of XAMPP. So the path will be: C:\XAMPP\htdocs\demo\index.php
  • Now, open index.php put in simple PHP code save it.
// index.php

<?php
echo "Hello World!";
?>

2. Run the server to test PHP

To run the serve, open your terminal/cmd and navigate to the folder we have just created. As you see in the above image path should be C:\xampp\htdocs\demo

Now put this line and hit the enter. php -S 127.0.0.1:8000

PHP 8.2.12 development server (HTTP://127.0.0.1:8000) started

You will instantly see the server has started message like the above image shows. Now you can open you browser to check the file.

To open the page enter the address on browser http://localhost:8000 or http://127.0.0.1:8000