PHP Scripting Language

This article aims at presenting an overview of PHP technology and its use as a server-side scripting language for developing dynamic web-based sites and applications. The article discusses about various features of PHP, overview of standard functions and extension libraries, serving as a starting point for developers who would like to know about PHP and intend to learn it.

What is PHP?

PHP is a recursive acronym for “PHP:Hypertext Preprocessor”(formerly PHP was called Personal Home Pages). It is a widely used Open Source scripting language that is especially suited for server-side applications and dynamic web content.

To give a brief history, initial version of PHP was developed by Rasmus Lerdorf in 1995. PHP has undergone lot of evolution since then. PHP interpreter was re-written based on a scripting engine called 'Zend Engine', which was developed by a company called Zend Technologies. Since 1998, Zend Technologies maintains PHP, and manages all new feature evolutions.The latest PHP version is 5, based on Zend Engine II.

PHP supports interaction with a large number of databases such as MySQL, Oracle, IBM DB2, Microsoft SQL Server, PostgreSQL, Firebird and SQLite. PHP runs on most major operating systems, including Unix, Linux, Netware, Windows, and Mac OS X, and can integrate with many major web servers. PHP is typically installed in conjunction with other open source software to build Web applications, a platform referred to by the term "LAMP" (which stands for Linux, Apache, MySQL, and PHP).

PHP scripts can be used from a command line interface without any web server or a browser.This type of usage is ideal for scripts regularly executed using cron or simple text processing tasks.

Why PHP?

PHP's syntax is similar to that of C, making it easy to learn and use for anyone with basic programming skills.

PHP has basic object-oriented features such as classes, attributes, operations, etc;however, the latest version of PHP PHP5 significantly improved the OO model to support private member variables and methods, static variables, inheritance, abstract classes etc.

PHP is open source, very well supported by a large developer community. The community gives excellent

technical support to users, and bugs are found and fixed quickly.

PHP is based on Zend engine, which offers PHP performance, reliability and scalability.

Very good support for XML parsing.

Widely used as a scripting interface by provisioning products

Basic Syntax

PHP scripts are always enclosed between opening and closing PHP tags. The three different forms are shown below. In the examples shown below, 'echo' and 'printf' functions can be used interchangeably

PHP's language syntax is similar to C's and Perl's. The variables need not be declared before using them. PHP denotes all of its variables by using the $ operator followed by any combination of characters. For example, $greeting = “Hello World”, $name=”Torry Harris”, $age=20.PHP is what is known as a "loosely typed" language.What that means is any given variable can be an integer, floatingpoint number, string, object, or an array.

The control structures in PHP are very similar to those used by the C language. PHP's control structures have two syntaxes that can be used interchangeably. The first form uses C-style curly braces to enclose statement blocks, while the second style uses a more verbose syntax that includes explicit ending statements. The first style is preferable when the control structure is completely within a PHP code block. The second style is useful when the construct spans a large section of intermixed code and HTML. Common control structures with dual syntax are if/endif, for/endfor, while/endwhile, switch/endswitch.

PHP provides three primitive data types: integers, floating point numbers, and strings. A string is a sequence of characters.A string can be delimited by single quotes or double quotes:'Hello, World' "Hello, World!"

An array is a compound data type that can contain multiple data values.PHP treats all arrays as hash-tables and the hash, or index, can be a string or a number.These kinds of arrays are called associative arrays:

Creating Dynamic Web Pages with PHP

PHP is well suited for developing dynamic web pages, handling HTML forms, using cookies, sessions, etc. PHP supports pre-defined associative arrays such as $_GET, $_POST to access form data. There are other pre-defined arrays such as $_COOKIE, $_SESSION to represent the list of cookies and HTTP session variables. The following login form example demonstrates the simplicity in accessing form fields.

In addition to the above example, PHP allows access to standard CGI variables such as REMOTE_ADDR (client's IP address), HTTP_USER_AGENT (client's browser info), etc. These variables can be accessed using $_SERVER global array. PHP also supports $_REQUEST associative array consisting of the contents of $_GET, $_POST, and $_COOKIE.

PHP built-in functions and extensions

There are several other PHP API functions provided by 3rd party vendors and developers.These functions are called as PHP extensions.These functions “extend” PHP's current function set. Extensions are built as dynamic libraries (.so or .dll) depending on the platform.The most useful extension modules ship with the normal PHP distribution such as Bzip, MySQL, etc. Developers can implement their own extensions using C on both Linux/Unix and Windows platforms.

To manage the packaging, distribution and aggregation of useful extensions developed by 3rd party developers, a public repository called PEAR (PHP Extension and Application Repository) exists. PEAR is comparable to Perl's CPAN network.

Case Studies

For one of our Telecom Customers, we had to develop an application using a provisioning product. The product supports PHP as the standard scripting interface. We used PHP to develop web-based administrative GUI. The product provides PHP extensions to interact with its objects.The following simple example demonstrates the use of extension to connect to the product's environment, create an object and execute an operation on it.

The above shown approach helped to exploit PHP's features to quickly develop administrative GUIs.It was also used to perform automated unit testing of the components from a command line environment using stand-alone PHP interpreter

Further Reading and Useful Links

PHP is widely used by web developers across the world. Hence, lot of useful utilities, frameworks, IDEs, articles exists on the web.The following links i've you pointers for further reading on PHP.