Page | 2

PHP: Hypertext Preprocessor

Justin T Pleva

Department of Computer Science

University of Wisconsin-Platteville

Abstract

Hypertext Preprocessor (PHP) is a server-side scripting language generally used to create dynamic web pages. Originally started in 1994 to help streamline basic website tasks, PHP has since become one of the most used website programming languages with nearly 20 million installations worldwide. PHP can be used as a general purpose scripting language to create command line and desktop GUI applications along with the dynamic web pages. PHP is also very versatile in which it can be installed and configured on nearly any operating system that can build from source code, but also comes prepackaged and pre-installed on many Linux distributions. PHP allows for end users to interact more with the web and create pages with less effort. Without PHP or other like server-side scripting languages, the web as we know it today would be much different.

Introduction

Initially PHP was designed to be a set of tools written in Perl to help streamline some simple tasks for the developer’s personal website. PHP was for that reason originally name “Personal Home Page Tools”, PHP for short. Later, around the time of the first public release in 1995, PHP was rewritten in C and aimed at streamlining various website tasks other than the original few tasks, specifically in web forms. Since then, PHP has grown in popularity and is currently one of the major back end scripting languages for website development claiming more than 20 million websites and domains.

History

PHP was first developed and used in 1994 by Rasmus Lerdorf. The original design was a set of Perl scripts that were used in order to help streamline some basic website tasks such as displaying a resume. Since then, PHP was rewritten using C and was released publicly for anyone to use. In 1997, PHP was rewritten again, this time by a team of 3 developers, one of which was the original developer. This new set of developers began rewriting the PHP code from scratch and released it as PHP 3 in 1998. This version also introduced the name change, which is currently in place today. The original PHP name which stood for “Personal Home Page Tools” of “PHP Tools” for short was renamed to PHP: Hypertext Preprocessor. Version 4 was released on May 22, 2000. PHP 4 introduced the Command line interpreter for another use of PHP rather than web pages only. Also in this version, the Zend engine was introduced. Zend is a runtime engine used to execute the PHP code. PHP 5 is built off of the Zend engine. PHP 5 was released on July 13, 2004 and is the current major release version as of today. As of version 5, the PHP developers began focusing more on minor release version rather than releasing a major version every year. This plan was originally started with version 4, but really took off with version 5. Major versions are defined as, for instance major release 3 of PHP was PHP 3. The minor releases are defined as, for instance PHP 5.4, minor release 4 so anything after the major version that’s not another major version.

PHP has brought on many notable changes. One of the major changes is that Object Oriented PHP was completely rewritten and expanded. Before version 5, Objects were instead copied as a whole when the user was doing any manipulation of the object. Now with version 5, true object oriented programming has been implemented. Many of the minor versions of PHP 5 have been for bug and security fixes. For nearly four years, PHP 4 and PHP 5 were released alongside of each other with PHP 5 being the mainstream version, and PHP 4 releases being minor releases. The most recent release of PHP is version 5.4, which was released on March 1, 2012. This version introduced performance increases as well as decreasing the memory usage. This version also removed a few items such as running PHP scripts in safe mode.

PHP 6 has been in the works for quite some time now, but has been put on hold due to some issues the developers are running into. One of the main issues is that PHP does not contain support for Unicode characters or multibyte strings. This issue was planned to be fixed with PHP 6. Since it was taking the developers much longer to include this native support, many of the other features due to be released with PHP 6 has since been released with earlier versions such as PHP 5.4.

Operating Environments

Since PHP is open source and the code is available for anyone to download, PHP can be used across nearly any operating system that supports compiling source code. With that said, PHP also comes as pre-compiled binaries for various operating systems such as UNIX, Solaris, Windows, OS/2, Linux and Mac OS X. In particular, Linux systems generally come bundled with PHP or will even have it installed by default. Many of these distributions are referred to as LAMP, MAMP, or WAMP stacks. For example, the LAMP stack refers to (L)inux (A)pache (M)ySQL (P)HP. These stacks will be discussed more in depth later on. Also, since PHP is designed to run on any system, this makes development much easier. If the programmer is writing his PHP script on a Windows PC, this script will run on a UNIX, Mac or any other system that PHP is installed on.

PHP is designed to run as an extension to various web servers to provide server-side scripting. Also, PHP has a command line interpreter to allow for command line applications and GUI applications making it a versatile scripting language. PHP will run natively with various web servers. PHP also is distributed as source code allowing it to be installed and configured on other, lesser-known systems. Some of the major web servers PHP supports by default are Microsoft’s Internet Information Services (IIS), Apache and Lighttpd.

Licensing

PHP is distributed under the PHP license, which states that the software is free of charge and can be redistributed as long as the source code remains intact. Also, the license states that the term PHP cannot be included in the title of any software product without permission from the PHP developers. For example, the popular web based MySQL database administration page, phpMyAdmin, which was written in PHP would be violating this license agreement as it uses “PHP” in the title, unless the developers had approval for this. However, the license states that PHP can be used in the title if the software is saying it works with PHP. For example, if the software package title is “Example for PHP”, this would be an acceptable usage.

Syntax

The syntax of PHP is very similar to C, Java and any other languages that are similar to the C convention. Like every other programming language, PHP requires some method of declaring that the text you are typing is PHP code.

Declarations

PHP code will only be executed if it is inside of the delimiters, which tell the PHP interpreter which region has PHP code, and which regions should be ignored. There are a few different ways to declare your PHP code. The first, and standard way is by using the opening “<?php” and closing “?>” tags. This method can also be done using the shorthand “<?” and “?>” tags.

Another method used to declare PHP scripts is by using the script tag <script language=”php”>. This method is not as standard as the first mentioned methods but does exactly the same thing. Also, PHP works with ASP style declaration tags such as “<%” and “%>”.

For example, to create a sample “Hello World” program using PHP, it would be as follows:

<?php

echo "Hello World";

?>

This example shows the standard usage, using the standard opening and closing tags. This same script would produce the exact same results using any of the other mentioned script opening and closing tags.

Commenting

PHP contains support for comments just like any other programming language. The difference however is that PHP supports multiple styles of comments. For example, In Java, C and various other languages, the programmer can comment either a single line, or a block of lines. In PHP, Java and C languages, this is declared by using either // for a single line, or /* */ for a block of lines. This is the same style among these three languages; the difference comes in with single line comments.

Functions

Function declarations and calls are very similar between C, Java and PHP. Functions can be either value returning or non-value returning. Also, functions can either have parameters or no parameters. PHP has thousands of built in functions, for example, in C++ we have “cout” to output to the console, or wherever the output is set to, in PHP the equivalent would be “echo”. The echo function will display the information to the display method, such as directly on to the web page.

A sample function is shown here:

function myFunction($value1, $value2)

{

return value1 + value 2;

}

Now for a value returning function such as the example above, to call this function you can either declare a variable, or you can directly call the function such as the following example:

<?php

echo “Value of the two variables is: “ . myFunction(3, 5);

?>

This example would produce the result of “Value of the two variables is: 8”.

Data types and Variables

In PHP, variables are declared by having a dollar sign ($) in front of some word. For example, a variable declaration would be “$testVariable” without the quotation marks. Now as you can notice from this example is there is no distinction of what type the variable is. In PHP data types are considered to be weak, that is, there is no distinct difference between a string, integer, or Boolean value. That being said, the programmer must make sure everywhere in the scripts that the variables are being used correctly and being cast as the correct data type otherwise the application could have some undesired results.

<?php

$thisIsAVariable = "1";

?>

The above example is declaring a variable and setting it equal to 1. The 1 in this case can be used as a string, an integer, or even a character value. This is one example of how PHP is different than C, Java, and other similar languages. The languages mentioned explicitly define values such as “int thisIsAVariable = 1” would define this as an integer value, and would throw an exception if it’s used as anything else.

Objects and Object Oriented Scripts

Since version 3, PHP is an object oriented scripting language. At first when object oriented was introduced into PHP it was very weak and didn’t have very good support. At this time, when a user called an object in their script, the entire object was copied causing larger amounts of memory to be used up, and the scripts to take a longer time to execute. Now, with PHP 5, objects were completely rewritten to increase the performance and objects are no longer copied each time. Also with PHP 5, objects were more like how they are in Java and C++. That is, they had constructors and destructors, had private and public members, abstract classes and even introduced some exception handling. For an example of a class in PHP, see appendix [A] below.

One difference between a PHP class and Java class is that when calling a class member in Java, it’s called using a period (.) where instead with PHP, a class member is called using an arrow ->. For an example, calling the class function myFunction, is shown below in both Java and PHP.

Java:

myVariable = myClass.myFunction();

PHP:

$myVariable = myClass->myFunction();

PHP classes are also very similar to Java and C++ as they can be inherited and can be abstract. For example, in PHP if you have a parent class called “Courses”, you have the ability to extend that class with a sub, or child class called “CS” for data members specific to Computer Science, while still having most of the members of the parent, “Courses” class. To extend a class, one simply uses the “Extends” keyword after that class title.

One new feature with classes that PHP 5 introduced was the namespace. A namespace allows for grouping of classes, or packaging them together. All classes inside of the same namespace are aware of the other classes and their public members as long as they are all within the same namespace and project. For an example of a class in PHP, see appendix [B] below.

File Extensions

Like every file that resides on a computer, PHP has its own unique file extension. However, what makes PHP different than another programming language such as C# is that it can have various file extensions. C# for example has the file extension of “.cs” and PHP has the default extension of “.php”. What sets PHP apart from the rest is not only can it use the .PHP extension, it can also use “.phtml” and “.php5”. In this example, the PHP5 extension will tell the PHP interpreter to use PHP version 5 if there is more than one versions installed on a system.

Usage

PHP can be used for a range of items with the most prominent of those being web applications and server-side scripting. PHP is more of a general purpose scripting language that can be used either to create web based applications, or it can be used on the client side to create GUI applications as well as command line operations. PHP code is generally aimed at being used as a server-side scripting language, similar to that of Microsoft’s ASP.

As PHP files are essentially nothing more than text files with a different extension, PHP acts more like a filter for these text files. PHP will intercept the commands when a file with a PHP extension is accessed by the web browser or the command line. Once PHP intercepts the files, it will process the text as a set of commands. Once these commands have been processed, PHP sends the resulting data back to either the web server or the command line that requested the operation.