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
XAMPP/htdocs
folder. - Open the XAMPP control panel and start Apache.
- Open the web browser and access
http://localhost/SCRIPT.PHP
- Alternatively, run the PHP script in the command line –
php PATH/TO/SCRIPT.PHP
That covers the quick basics, but read on if you need the exact steps and details!
TABLE OF CONTENTS
WHAT ARE XAMPP, SAPI, CGI, CLI?
Before we get into the step-by-step instructions, let us clear some confusion first. Most of you guys are probably wondering what XAMPP is and what are all of these crazy technical terms.
TUTORIAL VIDEO
RUN PHP IN COMMAND LINE (CLI)
Let us start simple, PHP is an independent programming language. We can download it from the official website, install it, and be done. Open the command line, run a PHP script with php PATH/TO/SCRIPT.PHP
.
RUN PHP IN WEBSERVER – SERVER STACK
If you want to “use PHP to create a website”, you have to install “HTTP server” software. The common ones in the market are:
Then, we need a database to store data. A few of the common ones are:
You 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 call a “server stack”. Very commonly in the industry, PHP is bundled as WAMP (Windows, Apache, MySQL, PHP), LAMP (Linux), and MAMP (Mac).
No need to worry though, some good people have thankfully bundled 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.
SERVER API (SAPI) & COMMON GATEWAY INTERFACE (CGI)
So far so good? The last thing you may bump into is SAPI and CGI. Let’s just say it’s “how PHP works with the HTTP web server”. Not going too deep into this, it’s another can of worms… Do your own research if you are interested.
RUN PHP THROUGH WEBSERVER
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
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.
EXTRAS
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
<?php
print_r($argv);
D:\http\>php test.php foo bar
Array
(
[0] => foo.php
[1] => foo
[2] => bar
)
In the “normal web environment”, we usually send data via the $_POST
or $_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.
LINKS & REFERENCES
Here are a few links to references and stuff, if you want to learn more.
- The official PHP manual
- For the guys who are stuck with IIS. Installing PHP on IIS – Tech Expert
THE END
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!