Blog

The basics of PHP – A must read for System Admin

Tags: basicscliinterpreterparserphpphp modesphp-cgisapiserver side scripting

Published on: June 11, 2010 by George K.

The basics of PHP – A must read for System Admin

Scenario:

PHP is similar to many other scripting languages like perl, python etc. But unlike perl and python what makes it stand apart is, its adaptability and power to be used as both command line and server side scripting.Here we trying to explain the PHP basics

I shall try to explain you the difference by executing the same file on different modes. Don’t expect too much from this post.

Command Line (CLI)

PHP Command Line Interface or PHP CLI  as the name implies,  is a way of using PHP in the system command line, like below.

In other words when the SAPI ( Server API) is “Command Line Interface” as you see above php-cli acts as a connector between underlying php binary and the command/script which invokes php from shell. This enables processing of the scripts which require php functions and provide the result after execution of the script. The advantage is that it doesn’t require a browser or webserver  for the execution. It simply requires a PHP parser ie PHP_CLI. This type of usage is ideal for scripts regularly executed using cron (on *nix or Linux) or Task Scheduler (on Windows). These scripts can also be used for simple text processing tasks. (Okay even image processing tasks :D)

PHP CLI is available on all popular operating systems. The first step we need to ensure is that the CLI SAPI is installed. For this locate php binary path and execute the command as given below

The result ensures that the php-cli module is installed. Now let us  see, how to make it work in a shell. The first thing is to identify the exact path on which php  binary is present. I assume that its /usr/local/bin/php.

Now create the test file info.php with the contents as shown below. You can use your favourite editor for this.

Execute the  file from the shell using the command line or shell. If you intent to use it in a cron specify the binary path (interpreter) as the first entry of file .

/usr/local/bin/php info.php

Then the out put will be  of the following  format

You can see the Application Programming Interface (API) for this script by checking the variable Server API. In the above test, it is shown as Command Line Interface. The value gets changed according to the mode of usage or execution.

As I mentioned above the php binary was  invoked  by the php-cli interpreter. Once php binary executed the query, the result was returned to the SAPI  ie php-cli, it then passed the results to the command line.

Why do we use it?

Its a quite handy option to create cron jobs. Some times we may need to perform some sort of updates or script execution periodically. If we set  a cron job for this using php, the php-cli option is quite useful.

Server-side scripting

PHP is extensively used for creating dynamic Web pages. You create pages with PHP and HTML. When a visitor opens the page, the server processes the PHP commands and then sends the results to the visitor’s browser along with the static HTML pages. PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document.

So the minimum requirement for here are the following ones

1.  Web browser ( client side)

2.  Web server

3.   The PHP parser :  which connects the webserver with the underlying php installation.

Illustration :

Now lets us, take the same example. Here I am trying to access the page through a web browser. Let us see how the result looks like

Here , you can see the difference. In the first case when I executed the same file from shell  I got the output without any format. Now the same result is shown as an HTML page.  So let us see  how the conversion takes pace.

The webserver is  connected to the php with the help of “Server API” . This is called PHP parser  it is either CGI or Server Module like apache, litespeed etc. You can see  it is “CGI” here, but it can vary according the mod of installation and webserver it uses. Common values are FastCGI/CGI , Litespeed API, Apache etc

Once the HTTP  / Web server gets the request for the page with php , it calls PHP interpreter to generate HTML. Then this HTML is returned to the client – internet browser which sent the HTTP request.

I believe you might got some ideas how it works or differs . I shall try to give  the difference between CGI and a server module on another post 🙂

Server API (SAPI)

PHP is meant  to work on all platforms. So it is essential that it should work with different types of webservers as well.   Every version of  php comes with a set of Binaries to connect php with various webservers and it is known as SAPI.  During configuration of php, we  need to give corresponding options for the webserver to which the interpreter is to be enabled. Once the php is compiled and installed , the corresponding SAPI  will be activated. So every communication between the webserver and PHP will be interpreted  using the corresponding SAPI module enabled.

Apache may use “Apache Handler” while LiteSpeed webserver use “LiteSpeed API” as the SAPI.

Extensions

Being a powerful language which can perform various levels of operation,  PHP needs to utilize various functions and libraries installed on the server. For example GD libraries are required for a php script which performs image manipulations. While a shopping cart or such applications require db connectivity to be enabled.  Like in the case of SAPI a lot of such connectors are shipped along with PHP.  To extend the functionality of php, we enable the necessary libraries while configuring the application using “–with”  option.

You can create your own php extensions and php modules and compile them also.  Again, we will have a discussion about it later.

Recommended readings on PHP basics:

https://www.supportsages.com/2010/06/php-behind-the-scenes/

https://www.supportsages.com/2010/06/mod_php-explained/

https://www.supportsages.com/2010/06/php-cgi/

Category : Apache, General, Howtos, Linux, Special Offers, Training, Troubleshooting

George K.

George K.

George started his career in web hosting and Linux technical support in the year 2004 and is with SupportSages since 2009. He has keen interest in server optimizations, custom security solutions, hacked server recovery, cyber forensic and high availability fail over system design and implementation. George loves long drives and is passionate about art and literature.

You may also read:

Comments

Add new commentSIGN IN

Let's Connect

Categories

Your Cart

Cart is empty.

Subtotal
₹0.00
APPLY
0
Send this to a friend