Welcome to a quick tutorial on how to run PHP scripts. So you are interested in learning PHP but realized that one does not simply double click a PHP file to run it. Neither does opening it in a web browser work. Just how do we run PHP files then?
The easy way to run PHP scripts is to:
- Download and install XAMPP on your computer.
- Put the PHP scripts in the
- Open the XAMPP control panel and start Apache.
- Open the web browser and access
- Alternatively, run the PHP script in the command line –
That covers the quick basics, but read on if you need the exact steps and details!
TABLE OF CONTENTS
|What Is XAMPP?||Webserver PHP||Command Line PHP|
|Useful Bits & Links||The End|
WHAT ARE XAMPP, SAPI, AND CLI?
Before we get into the step-by-step instructions, most of you guys are probably confused as to what XAMPP is and why we need it. Also, those who have been digging around may be wondering why there are so many different ways and guides to run PHP – Let us clear those doubts first.
RUNNING PHP IN THE COMMAND LINE (CLI)
Let us start simple, PHP is an independent programming language. We can simply download it from the official website, install it, and done – Just fire up the command line, run a PHP script with
Next, let us put PHP aside for a while first, and talk about web servers. To set up a web server, we need to install an “HTTP server” software. The common ones in the market are:
Then, we need somewhere to store data, in a database. A few of the common ones are:
Some of you guys probably see where this is going – We need a suite of software to run a web server, not just PHP alone. This suite of software is what we also call a “server stack”. Very commonly in the industry, PHP is bundled as WAMP (Windows, Apache, MySQL, PHP), LAMP (Linux), and MAMP (Mac).
RUNNING PHP IN A SERVER STACK – SERVER API (SAPI)
So far so good? For you guys who want to use PHP to build websites, we need to configure the HTTP server to work with PHP. This is called SAPI, and where the confusion is coming from – The settings are all different for Apache, IIS, Nginx, and whatever else is out there.
But thankfully, some good people have pre-bundle server stacks that you can download and deploy quickly – XAMPP (cross-platform, Apache, MySQL, PHP, PERL) is one of the more popular ones. No need to manually download Apache, MySQL, PHP and configure them one by one.
RUN PHP THROUGH WEBSERVER (SAPI)
With that, you should now know the story behind XAMPP – It is a pre-packaged web server stack. So let us now get into the steps of installing it, and running a simple PHP file.
STEP 1) INSTALL XAMPP
Head over to the XAMPP homepage and download the appropriate version for your operating system. Then run the installer and follow the installation wizard.
STEP 2) START APACHE & MYSQL
Next, fire up the XAMPP Control Panel, and this is where you can switch the components on or off. Usually, we only need the Apache web server and MySQL database server – So turn those on.
STEP 3) CREATE YOUR PHP SCRIPT
<?php echo "Hello World!";
If you have properly installed XAMPP, then the default folder for the HTTP documents will be located at
XAMPP/htdocs. Simply create your own “hello world” PHP script there.
STEP 4) RUN!
Finally, open your browser and navigate to
http://localhost/hello.php – Congratulations! You have successfully installed a web server stack and run your first PHP script.
RUN PHP IN COMMAND LINE (CLI)
With XAMPP installed, running PHP in the command line should pretty much be “Captain Obvious straightforward”.
STEP 1) LAUNCH THE COMMAND PROMPT
On Windows, search for “cmd” and run the command prompt. On Linux and Mac, this is called “terminal” instead.
STEP 2) RUN!
C:\>php c:\xampp\htdocs\hello.php Hello World!
That’s right, just run
php PATH/TO/SCRIPT.PHP. The end.
USEFUL BITS & LINKS
That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.
CHANGING THE HTTP ROOT FOLDER
#DocumentRoot "C:/xampp/htdocs" #<Directory "C:/xampp/htdocs"> DocumentRoot "D:/http" <Directory "D:/http">
Don’t like the default HTTP folder? You can change it by editing a few lines of configuration. Go to the XAMPP Control Panel, hit the config button, and choose “Apache httpd.conf”. Look for the document root section, and change it to wherever you like.
PASSING IN ARGUMENTS FOR COMMAND LINE PHP
D:\http\>php test.php foo bar Array (  => foo.php  => foo  => bar )
In the “normal web environment”, we usually send data via the
$_GET variables to the script. In the command line environment, we can also pass arguments in and access them via
$argv. Yep – Use this wisely for your own command-line application.
INFOGRAPHIC CHEAT SHEET
LINKS & REFERENCES
Here are a few links to references and stuff, if you want to learn more.
Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!