How to Run a PHP Script? Step By Step Guide!

Hello and welcome to the start of's series of web development articles!


Setting up a development environment for PHP programming is easy. Download the code editor you prefer, I personally like text editor.

Next is to install XAMPP, the most popular PHP development environment. This package contains Apache, PHP & MariaDB or MySQL database applications.

Many people emailed me with a main question: Mike, how to run a PHP script? This post is my answer to you guys and to those people who will need this in the future.

In the following tutorial, we will learn how to install XAMPP, how to run a PHP script, manage database with PhpMyAdmin and run a sample PHP script that fetches a record from the database.

Install XAMPP

Go to this link and download XAMPP for your operating system. XAMPP is available for Windows, Linux or Mac.

Here's a video about how you can install and use XAMPP.

Run Your First PHP Script

The following is an example about how to run a PHP script. What this program does is show a "Hello World!" text on the screen or webpage.

Go to XAMPP server directory

I'm using Windows, so my root server directory is "C:\xampp\htdocs\".

Create hello.php

Create a file and name it "hello.php"

Code Inside hello.php

Open hello.php and put the following code.

echo "Hello World!";

Open New Tab

Run it by opening a new tab in your browser

Load hello.php

On you browser window, type http://localhost/hello.php


You should see the following output.
Great job, you just run a PHP script!

Manage MySQL with PhpMyAdmin

MySQL is an open-source relational database management system (RDBMS). MySQL is a popular choice of database for use in web applications.

phpMyAdmin is a free and open source tool written in PHP intended to handle the administration of MySQL with the use of a web browser. In the following examples, we will see how easy we can handle MySQL with PhpMyAdmin.

Create a Database

  1. Go to http://localhost/phpmyadmin/
  2. Click the "New" link on the upper left corner (under recent tables)
  3. Fill out the "Database Name" field with "my_first_database".
  4. Click the "Create" button

Create a Table

  1. Click "my_first_database" on the left side of the screen
  2. On the "Create Table" section, fill out the Name with "products" and Number of Columns with "6"
  3. Click "Go" button
  1. Fill out the fields with id, name, etc.
  2. Mimic everything in the following image
  3. Click the "Save" button

Insert Data

Click the "products" table.


Click the "Insert" tab.


Fill out the form, mimic the data on the following image. Click the "Go" button.


Great job! We now have a database, a table inside the database and a record inside the table.


Useful Videos

1. Create a database and import MySQL file.

2. Create a database and create table.

Run PHP Script with Database

In the following steps, we will run a PHP script that fetches one record from the MySQL database.

Go to XAMPP server directory

Go to your "C:\xampp\htdocs\" directory

Create read_one.php

Create a file and name it "read_one.php"

Code Inside read_one.php

The numbers 1-8 in the following code are called "code comments". It explains each part of our simple code below. Open read_one.php and put the following code.

// 1. database credentials
$host = "localhost";
$db_name = "my_first_database";
$username = "root";
$password = "";
// 2. connect to database
$con = new PDO("mysql:host={$host};dbname={$db_name}", $username, $password);
// 3. prepare select query
$query = "SELECT id, name, description, price FROM products WHERE id = ? LIMIT 0,1";
$stmt = $con->prepare( $query );
// 4. sample product ID
// 5. this is the first question mark in the query
$stmt->bindParam(1, $product_id);
// 6. execute our query
// 7. store retrieved row to the 'row' variable
$row = $stmt->fetch(PDO::FETCH_ASSOC);
// 8. show data to user
echo "<div>Name: " . $row['name'] . "</div>";
echo "<div>Description: " . $row['description'] . "</div>";
echo "<div>Price: $" . $row['price'] . "</div>";

Open Your Browser

Run it by opening you your browser

Load read_one.php

On you browser window, type http://localhost/read_one.php


You should see the following output.
Awesome! You are now ready to learn more about web programming and development.

Online Resources

Here in, we want to simplify learning for you to actually build something. But it is also important for you to read and study more. The following are my suggestions where to learn more.

You can always go back to the list above while you go along our series of web programming tutorials.

What's Next?

Learn our JavaScript tutorial for beginners.

Related Tutorials

Please explore more tutorials on our start page. Click here.

Some Notes

Found An Issue?

If you found a problem with this code, please write a comment below. Please be descriptive about your issue. Please provide the error messages, screenshots (or screencast) and your test URL. Thanks!

Before you write a comment, remember to read this guide and our code of conduct.

Subscribe to CodeOfaNinja

We constantly improve CodeOfaNinja. We update our tutorials and source codes. Receive valuable web programming tutorials and updates to your email. Subscribe now!

Enjoy high-quality web programming tutorials.
Subscribe to CodeOfaNinja now for FREE!

Thank You!

Please share this post if you think this is a useful tutorial about how to run a PHP script. Thanks for learning here in!