Home » Learn Code » PHP » How to Run PHP Code on Your PC

How to Run PHP Code on Your PC

  • PHP
  • 6 min read
How to Run PHP Code

Do you want to run PHP code on your PC? This tutorial will teach you everything.

PHP is a server-side scripting language, that’s why you can’t run it by just opening it in your browser.

You need two things to run PHP code on your PC.

  1. A Local Webserver with Apache, MySQL, and PHP support,
  2. A Text Editor like Visual Studio Code.

Note: You can use any text editor with PHP support.

Now, let’s see the steps to set up, install and run PHP code on your PC.

Get Started with PHP

The first thing, you need to do is, create a local web server on your computer to run PHP. To do this, you need XAMPP. So, let’s start with our first step.

Install XAMPP, Run Apache and MySQL

XAMPP is software that you can use to create a local webserver on your PC. Using XAMPP, you can run the Apache server that is required to run and execute PHP code.

There are other web server apps available on the internet like WAMP, MAMP, etc. But we choose XAMPP because it is open-source software and available for more than one operating system. It is available for Windows, Linux, and MAC OS.

To install XAMPP, visit the apachefriends.org website. Then, download and install XAMPP on your PC.

install XAMPP to run PHP

After installing XAMPP on your PC, open it. Then run Apache and MySQL server as shown in the image below. MySQL is a database server, it is not mandatory. Because in this tutorial, we’ll not talk about the MySQL database. We’ll talk about the MySQL database in our later PHP tutorials.

So you can only Start Apache if you want.

Run Apache and MySQL on XAMPP

And done! you have been successfully created a local web server on your PC. But wait! How you can access it in your browser?

Every web server has an IP address and a hostname. As this server is on our local computer. So, for XAMPP by default, our hostname is localhost and our localhost’s IP address is 127.0.0.1.

Just type localhost in your browser’s search bar to access your local web server.

localhost

You will see a Welcome to XAMPP page. This means the local web server or XAMPP is working on your PC.

Remember: On your web server your website files are located in directories (mean folders). Same as the files and folders on your computer. So, here in XAMPP, your website files will store in the htdocs folder which is located in the XAMPP folder. As shown in the image below.

htdocs folder in xampp to store php files

This is where your HTML, CSS, JS, and PHP files folder will store. The PHP files you’re going to create in the next step will store in this folder.

So now, we have a web server on our PC. Now we can create a PHP file and execute PHP code in our Browser. So, let’s see our next step.

Create a PHP File to Run PHP Code

I am using visual studio code to create a PHP file. You can download this text editor from their website, it’s free.

Before creating a PHP file, open the htdocs folder and create a new folder in that. You can give it any name. This is the folder where your website or project files are located. As you can see in the image below, I gave it a name testwebsite.

Create a Folder in htdocs folder

You can access this folder in your browser by typing localhost/testwebsite.

Now, open visual studio code and open this folder in your text editor.

Open test website folder in VS code

Then, create a PHP file and give it a name sample.php. Then, write some code. You can copy/paste the code given below.

<?php
  echo "Hello World";
  
 ?>

This will look like this as shown in the image below.

It is the simple hello world program that we’re going to run on your PC.

Now, we have created the PHP file. It’s time to run PHP in your browser.

Execute or Run PHP Code in Your Browser

Just make sure XAMPP is running.

Then open your browser and type in the search bar localhost/testwebsite/sample.php. And hit enter. This will open a PHP file and run a PHP script in your browser.

According to our code. The ‘Hello World’ program has been executed. See the image below.

a simple php hello world program

That’s it. Now, you know how to run PHP code on your PC.

This way you can create and run as many PHP files as you want.

Now next, you should learn PHP variables. Variables are containers for storing data. These will help you more in PHP programming.

If you want to learn something about Advanced PHP? See this trending guide on how to connect HTML Form to MySQL database using PHP.

Keep learning and sharing!

Leave a Reply

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