The very basics of a PHP – Not for the developers or sysadmins but for a tech support engineer

What is PHP?

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.  I hear Yahoo’s mail runs on PHP.

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.

# php -i | more
phpinfo()
PHP Version => 5.2.10-2ubuntu6.3

System => Linux den 2.6.31-15-generic #50-Ubuntu SMP Tue Nov 10 14:54:29 UTC 200
9 i686
Build Date => Nov 26 2009 14:40:20
Server API => Command Line Interface
Virtual Directory Support => disabled
Configuration File (php.ini) Path => /etc/php5/cli
Loaded Configuration File => /etc/php5/cli/php.ini

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

[george@server3]$ type php
php is /usr/local/bin/php
[george@server3]$ /usr/local/bin/php -v | grep cli
PHP 5.2.9 (cli) (built: Jul 10 2009 19:22:08)

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

vi info.php


<?php
phpinfo();
?>

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


[geroge@server3]# php info.php | head
phpinfo()
PHP Version => 5.2.9

System => Linux server3.xxxxxx.xxx 2.6.18-128.7.1.el5 #1 SMP Mon  Aug 24 08:21:56 EDT 2009 x86_64
Build Date => Jul 10 2009 19:17:35
Configure Command =>  './configure'  '--enable-bcmath'  '--enable-calendar' '--enable-dbase' '--enable-ftp'  '--enable-gd-native-ttf' '--enable-libxml' '--enable-magic-quotes'  '--enable-mbstring' '--enable-pdo=shared' '--enable-soap'  '--enable-sockets' '--enable-zend-multibyte' '--enable-zip'  '--prefix=/usr/local' '--with-curl=/opt/curlssl/'  '--with-tidy=/opt/tidy/' '--with-ttf' '--with-xmlrpc'  '--with-xpm-dir=/usr' '--with-zlib' '--with-zlib-dir=/usr'  '--with-litespeed'
Server API => Command Line Interface
Virtual Directory Support => disabled
Configuration File (php.ini) Path => /usr/local/lib
Loaded Configuration File => /usr/local/lib/php.ini

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

info_php

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.

Post to Twitter Tweet This Post

Related posts:

  1. Recompile PHP for Litespeed webserver
  2. Source compilation of Apache
  3. PostgreSQL for the sage – Must know basics for the system administrators
  4. mod_php explained
  5. Special Offer : Experience the QUALITY of SUPPORT for just $1 per ticket

Comment Form

About this blog

This blog, acts as a knowledge repository for the world and is unofficial! Anything we find interesting in the cyber world will go here. Most cases, this blog will reflect the happiness of our staff in reaching successful solution to an issue (s)he worked on. A reference for other fellow SAGEs who come across similar issues later