How to Run a PHP Script? Step By Step Guide!
Hello, and welcome to the start of codeofaninja.com's series of web development tutorials!
Overview
Setting up a development environment for PHP programming is easy. Download your preferred code editor, and I like the Visual Studio code 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 primary question: Mike, how to run a PHP script? This post is my answer to you and those needing this in the future.
In the following tutorial, we will learn how to install XAMPP, run a PHP script, manage the 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 of how to run a PHP script. This program shows a "Hello World!" text on the screen or webpage.
Go to the 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.
<?php
echo "Hello World!";
?>
Open New Tab
Run it by opening a new tab in your browser
Load hello.php
On your browser window, type http://localhost/hello.php
Output
You should see the following output.
Great job, you just ran 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 a web browser. In the following examples, we will see how easily we can handle MySQL with PhpMyAdmin.
Create a Database
- Go to http://localhost/phpmyadmin/
- Click the "New" link in the upper left corner (under recent tables)
- Fill out the "Database Name" field with "my_first_database".
- Click the "Create" button
Create a Table
- Click "
my_first_database
" on the left side of the screen - On the "Create Table" section, fill out the Name with "products" and the Number of Columns with "6"
- Click the "Go" button
- Fill out the fields with id, name, etc.
- Mimic everything in the following image
- 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 files.
2. Create a database and create a 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 the 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.
<?php
// 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
$product_id=1;
// 5. this is the first question mark in the query
$stmt->bindParam(1, $product_id);
// 6. execute our query
$stmt->execute();
// 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 your browser
Load read_one.php
On your browser window, type http://localhost/read_one.php
Output
You should see the following output.
Awesome! You are now ready to learn more about web programming and development.
What’s Next?
When building web applications, a presentable user interface is a must. We do not want our users to have a hard time using what we built.
So, for our next tutorial, we will learn a CSS framework called Bootstrap. It is easy to learn, and we will use it for the PHP web application we build in this series.
Click the following link for the next tutorial: Bootstrap Tutorial for Beginners
Online Resources
Here at codeofaninja.com, we want to simplify learning and for you to build something unique on the web. But it is also essential for you to read and study more. The following are my suggestions for where to learn more.
- PHP Manual - official PHP documentation.
- PHP The Right Way - easy-to-read, quick reference for PHP popular coding standards and more.
- PHP Delusions - proper use of PHP programming language, proper use, with the focus on disproving various delusions and superstitions.
- PHP Cheat Sheets - Variable comparison, arithmetic and testing cheat sheet.
- PHP FAQs - Frequently asked PHP questions
- PHP Security Cheat Sheets - prevent attacks like XSS, SQL Injection and more.
- Cross-Site Scripting Attacks (XSS) - Another good resource to learn and prevent cross-site scripting (XSS).
You can always go back to the list above with our web programming tutorials.
What students say?
Don't just take our word for it. See what our students have to say about our tutorials and source codes. We are proud to have helped many individuals and businesses to build their own applications. Here are a few of the testimonials from our satisfied students.
★★★★★ “Wow, I love you guys! The best web programming tutorial I’ve ever seen. So comprehensive, yet easy to follow. I love how you combine all necessary elements in such a neat structure.” ~ Olaug Nessa
★★★★★ “The fact that you’ve put it all together saves so much time and its worth buying the code. Makes me feel good supporting a developer like yourself. Keep up the good work!” ~ Dan Hudson
★★★★★ “Thanks for making these awesome tutorials! I bought your source codes. To be honest, it’s very readable code and helps me understand a lot of things and how it’s done in PHP. Thanks for that again.” ~ Michael Lammens
★★★★★ “Hey Mike, my name is Leonardo from Argentina. I’ve been reading your blog since like 4 months from now, and I really must say: your tutorials are very good, they has helped me in many of my works… Well, thank you very much man. I really admire your work.” ~ Leonardo
★★★★★ “Words can’t express how grateful I am for the work and the articles you post, had some troubles with doing somethings but your articles as per usual hit the hammer right on the head. They are a great way for expanding upon later too!” ~ Jeremy Smith
Got comments?
At codeofaninja.com, we strive to provide our readers with accurate and helpful How to Run a PHP Script? Step By Step Guide! Your feedback is essential in helping us achieve this goal.
If you have encountered any issues with the code, have suggestions for improvement, or wish to provide praise, we welcome you to leave a comment below. Please be as descriptive as possible to address your concerns effectively and include any relevant error messages, screenshots, or test URLs.
We request that comments remain on-topic and relevant to the article above. If your question or comment pertains to a different topic, we recommend seeking assistance elsewhere.
Furthermore, we ask that you review our code of conduct before commenting to ensure that your feedback is constructive and respectful.
Thank you for taking the time to provide feedback and for supporting codeofaninja.com. Your contributions help us improve our tutorials and serve the developer community better.
Subscribe for FREE!
Improve your web development skills and stay ahead of the competition by subscribing to our tutorial series. Sign up for FREE and access exclusive, cutting-edge content delivered straight to your inbox.
Take advantage of the chance to elevate your skills and advance your web development career. Subscribe now.
Thank You!
We hope you've found our How to Run a PHP Script? Step By Step Guide! helpful and informative. We understand that learning new programming concepts can be challenging, but we're glad we could make it easier for you.
Thank you for choosing to learn with us and for supporting codeofaninja.com! Consider sharing this tutorial with your friends and colleagues who may also be interested in learning about How to Run a PHP Script? Step By Step Guide!
The more people know about our tutorials, the more we can help the developer community grow. Keep learning, keep coding, and keep growing as a developer. We can't wait to see what you'll create next!
Hi! I'm Mike Dalisay, the co-founder of codeofaninja.com, a site that helps you build web applications with PHP and JavaScript. Need support? Comment below or contact [email protected]
I'm also passionate about technology and enjoy sharing my experience and learnings online. Connect with me on LinkedIn, Twitter, Facebook, and Instagram.