How to Run PHP Scripts – SAPI (Web) & CLI (Terminal)

Welcome to a beginner’s tutorial on how to run PHP scripts. So you may have some interest in learning PHP, but realized that one does not simply double click a PHP file to run it… Neither does directly opening it in a web browser work. Just how do we run PHP files then?

Long story short, there are 2 ways to run PHP scripts:

  1. Server API (SAPI) – Run PHP through a webserver. The more “commonly known” way amongst beginners.
    • Install XAMPP on your computer.
    • Create 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
  2. Command Line (CLI) – Run PHP “independently” without a web server and browser.

That should cover the basics, but if you need more details – Read on!

 

 

REAL QUICK GUIDE

 

TABLE OF CONTENTS

SAPI PHP CLI PHP Useful Bits & Links
The End

 

 

SAPI PHP (WEBSERVER)

All right, let us now get started with the more commonly known method of “running PHP” – Setting up a webserver.

 

STEP 1) INSTALL XAMPP

Head over to the XAMPP homepage, simply download and run the installer. This package consists of the full webserver stack – Apache, MySQL, PHP, and PERL. It is the fastest and easiest way to set up a webserver.

 

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 will only need the Apache web server and MySQL database server – So turn those on.

 

STEP 3) CREATE YOUR PHP SCRIPT

XAMPP/htdocs/hello.php
<?php
echo "Hello World!";

If you have properly installed XAMPP, then the root folder for the HTTP documents will be located at XAMPP/htdocs. Simply create your own “hello world” PHP script there.

 

 

STEP 4) “RUN PHP” FROM THE BROWSER

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.

 

EXTRA) CHANGING THE HTTP ROOT FOLDER

httpd.conf
#DocumentRoot "D:/xampp/htdocs"
#<Directory "D:/xampp/htdocs">
DocumentRoot "D:/http"
<Directory "D:/http">

Don’t like the default root 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.

 

 

CLI PHP (COMMAND LINE)

Next, this is for you guys who want to “run PHP only”. That is, running PHP independently without a web server nor browser.

 

STEP 1) INDEPENDENT PHP INSTALLATION

Actually, the easy and lazy way is still installing XAMPP. Just don’t start the Apache web server, and only use PHP/MySQL. But if you want to install PHP from scratch:

  • Windows users –
    • Download PHP from the official PHP website.
    • There are a few different versions to choose from, the thread-safe or non-thread-safe version does not really matter here when the webserver is not in the equation.
    • Just get the appropriate X64 (64-bit) version, or X86 if you are still on 32-bits Windows.
    • Lastly, remember that you will also need to install the Visual C++ Redistributable packages.
  • Linux users
    • Just run sudo apt-get install php
    • Or sudo yum install php… Whichever your package manager is.
  • Mac users
    • Good news for the Mac users, PHP is already pre-installed.
    • If you need to upgrade to the latest version, run in the terminal: curl -s https://php-osx.liip.ch/install.sh | bash -s X.X.
    • Replace X.X with the latest version. For example, 7.2 or 7.3. That’s it.

 

STEP 2) PHP.INI CONFIGURATIONS (WINDOWS ONLY)

For the unfortunate Windows users – The PHP download comes in a zip file. Just unzip to wherever you want, open up the php.ini file, change the extension directory accordingly.

extension_dir = "D:\php\ext"

Next, change the email settings if you plan on using it.

[mail function]
; For Win32 only.
SMTP = mail.site.com
smtp_port = 25

; For Win32 only.
sendmail_from = john@doe.com

 

STEP 3) SET SYSTEM PATH (WINDOWS ONLY)

Another unfortunate step for Windows users. Start > Search and open “System Environment Variables” > This will open up “System Properties” > Click on “Environment Variables” > Under “System variables” section > Edit Path > Add DRIVE:\PHP (where you have unzipped PHP into) > OK.

 

 

STEP 4) CREATE A PHP SCRIPT AND RUN

Finally, just create a test script and run it in the command line (or terminal).

test.php
<?php
echo "Hello World";
Command Line
E:\>php test.php
Hello World!

 

EXTRA) PASSING IN ARGUMENTS

In the “normal web environment”, we can send  POST or GET variables to the script. In the command line environment, we can also pass arguments in and access them via $argv:

test.php
<?php
print_r($argv);
Command Line
E:\>php test.php foo bar
Array
(
    [0] => foo.php
    [1] => foo
    [2] => bar
)

Yep – Use this wisely for your own command-line application.

 

USEFUL BITS & LINKS

That’s all for the code, and here is the download link as promised – Plus a small extra that may be useful to you.

 

YOUTUBE TUTORIAL

 

INFOGRAPHIC CHEAT SHEET

How To Run PHP Scripts – SAPI and CLI (click to enlarge)

 

LINKS & REFERENCES

Here are a few links to references and stuff, if you want to learn more.

 

THE END

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *