In the previous tutorial you have learned, PHP is a server-side scripting language. That’s why PHP must have a web server environment in order to execute and show output. That’s why you will need some tools & software utility set up on your local computer or your remote hosting. Because PHP can’t be run directly through a browser like a markup language and stylesheet languages such as HTML or CSS. To execute the result or output of a PHP code you will need set up a web server environment on your computer.
So exactly how and where can we write and run PHP code for learning and testing purpose or development purpose? Actually, you can do this by one of two following procedure explained below:
1. You can do this by using a web host that supports PHP
If you buy a web hosting package that is fully activated for PHP development. These days all the modern web hosting provider is PHP supported. Anyway, login to your cPanel and then go to your hosting file manager from cPanel then create and upload the PHP file you want to test. That’s it. now you can test your PHP code by typing the PHP file directory in the address bar of any web browser with an internet connection.
For example, if your hosting is configured on the domain:
www.example.com and if you put your desired PHP at the root directory as
text.phpthen you have to type in address bar
example.com/test.php It should execute the output of the file.
Disadvantages of this procedure
- You must have an internet connection to test your PHP code output with your remote hosting server.
- If you are not going to deploy a live website/web application on your hosting server. Then don’t purchase a hosting plan only for testing and development purpose. Because it will cost you dollars per month.
- If you use cellular networks or any limited data plan. It will cost you internet data volume.
- It will be much slower than your localhost (localhost is the second procedure that we will explain in the next section)
To avoid these problems noted above you have a solution. We can use our computer alone to run and execute PHP codes even faster than web hosting. But before running PHP code we must have configured your computer in order to support PHP. This configuration is conventionally called localhost configuration. So let’s set up localhost in our computer
2. Localhost Configuration
Before setting up localhost in our computer we will need some software utility. It will create a server on our computer. Components we will need:
- PHP Core Package: Before writing and testing PHP script you have to download and Install PHP core module on your computer. PHP is open source, everyone can use this language freely. You can download the latest version of PHP from here: http://php.net
- Web server: PHP works in the backend of a website or a web application. But in order to process the backend task PHP needs a web server. There are many servers you will find such as MS Internet Information Server (IIS), NiNgx web server etc. But widely and most used web server specially for PHP is Apache web server. It is a free and open source server everyone can use freely. Download the Apache Server module from here.
- Database: Every web developer often uses a database management system in order to store website data and information. In this tutorial series, we will work often with the database. So we also need a database software. There are many database software available on the web. Such as Oracle database, Sybase etc. But MySQL most and widely used database. We will prefer to use MySQL. Download the MySQL zippy file from here.
What is next? If you download and install that software from the above links. Still, the server won’t work. Because you have to configure this software in order to work together. But the configuration is lil bit tricky and technical. So, in this case, we will install such a software application that will install PHP, Apache web server and MySQL database at once. It will be configured automatically as well. You can use XAMPP to get this job done.
XAMPP stands for X=Cross platform, A=Apache web server, M=MySQL, P=PHP, P-Perl. After installing XAMPP on your computer all the necessary things will be installed. Then your computer will be ready to execute PHP code also. Download the latest version of XAMPP from here: https://www.apachefriends.org/download.html
After running the installer keep clicking on the next button. XAMPP will install. Now from the program list, run the XAMPP control panel. You will get an interface looks like this:
From the control panel Click on the first two start button to start Apache web server and MySQL database. Your local server called localhost will start working. To check the server working or not go to any of your web browsers and type
127.0.0.1 then hit enter. If you see a welcome message from XAMPP it will ensure your localhost server is running.
To stop the local server on your computer simply go to your XAMPP control panel and click n the first two stop button.
Running a PHP File Through XAMPP
After successfully installed XAMPP on your computer now we will run a PHP file through the Apache server for demonstration purpose. Now create a PHP file by any of your text editors (e:g: Notepad++). If you don’t know about PHP file. Then copy and paste the below code in your text editor then save it as
test.php. Make sure the file name extension is .php.
<?php #It will exexute and send back to browser as "Hello, world1" echo "Hello, world!"; ?>
Now move this file to
C:\xampp\htdocs this directory or the corresponding folder where you installed the XAMPP software. Go to your XAMPP control panel and start the Apache web server and MySQL database. Then go to your favorite browser and type in address bar
http://localhost/test.php then hit the enter button. You should get the output
Tips: By following the above way you can test any PHP file. Every time you want to test a file, create the PHP file in the Htdoc folder under xampp folder on your C drive. Then run it through a web browser by giving the file address. But always make sure your XAMPP server is running.
What is next?
In the next tutorial, we will learn the conventions of PHP code. Then we will learn how to write and test PHP script using XAMPP.