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.
- A Local Webserver software with Apache, MySQL, and PHP support,
- A Text Editor for example 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.
How to Run PHP Code
First of all, we’re going to teach you how to create a local web server on your computer to run PHP. Because PHP is a server-side language and you can’t run it without a server.
Fortunately, we have third-party software available on the internet like XAMPP, WAMP, MAMP, etc. These apps can make the job extremely easy for you to run PHP.
We’ll use XAMPP to create a local webserver. The reason I prefer XAMPP is that we can run XAMPP on multiple operating systems, like Windows, MAC, Linux, etc. And also, it has more features.
So, let’s begin with our first step.
Step 1: 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.
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.
And done! you have 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.
You will see a ‘Welcome to XAMPP’ page. This means the local web server or XAMPP is working on your PC.
Step 2: Place Your Project Folder on the Local Server
On a live web server your website files (aka your project files) are located in directories (mean folders). Same as the files and folders on your computer. Here, to run PHP code in XAMPP, your website files will store in the htdocs folder which is located in the XAMPP folder.
To do this, you have to copy-paste your PHP project folder into ‘htdocs‘ folder inside XAMPP to run your PHP code. If you’re new to PHP then just go to ‘htdocs‘ folder as shown in the image below and create a folder inside it with any name.
This is where your HTML, CSS, JS, and PHP files will store. The PHP files you’re going to create in the next step will also store in this folder.
So now, you have a web server running on your PC. It’s time to create a PHP file and execute PHP code in our Browser. So, let’s see the main step.
Step 3: 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
You can access this folder in your browser by typing localhost/testwebsite.
After that, open visual studio code and open this folder in your text editor as shown below.
Then, create a PHP file and give it the name sample.php.
After that, write some code in that file. 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 and written some code in it. Now, you can run PHP in your browser.
Step 4: Execute or Run PHP Code in Your Browser
To do this, 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.
That’s it. Now, you know how to run PHP code on your PC. And you can create and run as many PHP files as you want.
Things to Remember Before Running PHP Code:
- Whenever you want to run PHP code, always make sure XAMPP is running with Apache and MySQL.
- Your project folder containing PHP files must be placed inside ‘htdocs‘ folder that is located in the XAMPP installation folder.
- Whenever you install XAMPP on your PC, just make sure you select the default location to install XAMPP which is usually C drive. To avoid errors.
What’s next? Now, you should learn PHP variables. Variables are containers for storing data. This tutorial is will help you to understand the concepts behind variables.
If you want to learn something advanced about PHP. Then, see this trending guide on how to connect HTML Form to MySQL database using PHP.
That’s it, if you have any questions related to this guide, then feel free to leave a comment below.
Keep learning and sharing!