PHP-Paper

advertisement

1

Stephen Franchak

CMPS 344

Professor Jackowitz

Due 2/15/2012

PHP: Hypertext Preprocessor

Historical Background

Danish programmer Rasmus Lerdorf began developing PHP in 1994 by creating a set of

Perl scripts to maintain his personal home page. Originally calling this collection of Perl scripts

“Personal Home Page Tools,” Lerdorf produced these scripts in order to track the visitors who viewed his online resume. Lerdorf rewrote PHP Tools as a set of Common Gateway Interface

(CGI) binaries in the C programming language that he released in June of 1995. This early version of PHP had simple features that allowed users to interact with a limited number of database management systems and process information entered into Web forms.

In September of 1995, Lerdorf dropped the name PHP in his next rendition of the product and simply referred to it as FI for “Form Interpreter.” FI automatically interpreted Web form data, had variables similar to Perl, and could be embedded directly into HTML documents albeit having to be “hidden” by HTML comments. Although modeling the dynamic, weak typing of variables in Perl, variables in FI (and today in PHP) only have one sigil, the dollar sign, preceding variable names regardless of which data type the variable references. Until October of

1995, FI was only regarded as a set of CGI tools. Lerdorf began to change that perspective by rewriting the code so that it resembles the C language. He also brought back the PHP name and referred to his language as “Personal Home Page Construction Kit.”

Lerdorf completely revamped the language again in 1996 and referred to the language as

PHP/FI. PHP/FI included features such as built-in support for a larger number of database management systems, cookies, and user-defined functions. Even before PHP/FI 2.0 left beta in

November of 1997, the underlying parsing engine was being completely rewritten by Andi

Gutmans and Zeev Suraski, who were both developing an electronic commerce application and found several shortcomings in PHP/FI. Gutmans and Suraski eventually teamed up with Lerdorf, and together they formed a new independent language simply called PHP, a recursive acronym meaning “PHP: Hypertext Preprocessor,” that was launched in 1998. This release is also known as PHP 3.0.

Today, PHP 5.3.10 is the current version of PHP. Since PHP 3.0, PHP has been developed as an open-source product and maintained and implemented by the PHP Group. PHP is used on Web servers by over twenty million domains and over one billion IP addresses. PHP is the sixth most popular programming language according to the TIOBE Programming

Community Index for February 2012, having only gone down in rank by one place since

February of 2011. Once an incomplete and insufficient set of CGI tools that aided in creating simple dynamic websites, PHP has successfully evolved into today’s dominant server-side scripting language.

2

Overview

PHP is a general-purpose server-side scripting language mainly used for developing dynamic Web pages and Web-based applications. PHP code is embedded in an HTML document or a file with the file extension .php by being placed between <?php ?> tags. Any PHP code located inside of these tags will be interpreted by the PHP runtime environment on the Web server when the given document is requested. Most PHP code that prints output generates HTML code. Some PHP scripts generate and build entire HTML documents whereas other PHP scripts generate sections of an HTML document. PHP code that is embedded in an HTML document is never visible to the Web browser that requested the document. For instance, a PHP script can connect to a database, access the values of the fields in a row of a table in the database, and print that information. The Web browser obtains an HTML document that contains text containing the values of the accessed tuple. None of the PHP source code such as code snippets containing login credentials for the database is revealed.

PHP is an imperative language and an object-oriented language. Support for objectoriented programming was added later in PHP’s lifetime. The semantic appearance of PHP’s classes is similar to Java’s classes. There are a few noteworthy differences. PHP does not support some of Java’s language constructs such as abstract classes and interfaces. Instead of using a dot

( .

) as the syntax to access the methods and fields of some class, PHP uses -> to access instance methods and properties and :: to access static methods and properties. Despite these objectoriented features, PHP is executed procedurally. A PHP script does not begin execution by entering a main method within some class. Instead, classes are initiated, manipulated, accessed, etc. from the start of a PHP script. On the other hand, PHP does have garbage collection and exception handling similar to Java.

PHP is different than Java in a lot of other regards. Variables in PHP are dynamically typed. A dynamically typed variable does not have its data type associated with it at compile time, and hence there is no compile-time type checking. Whenever a variable is declared in Java, the data type of that variable must precede the variable’s name. The javac compiler would not allow an integer to be assigned to a variable bounded to the data type of the String class. This assignment would be permitted without complaint by the PHP interpreter. Also, character strings and arrays have dynamic length. In PHP, the variable $a can be declared and the fifth cell in an implicit array can be assigned the integer 5 by the single statement $a[4] = 5; . An array object does not have to be declared like they need to be in Java. In addition, PHP has associative arrays. One can index into an array by using a string in a way conceptually similar to the hash table or dictionary data structure. In Java, one can only index into an array by using an integer value greater than or equal to zero and less than the size of the array. Arrays and strings in PHP are mutable.

PHP uses a language construct known as superglobal arrays to access information from

Web forms ( $_GET or $_POST ), user sessions ( $_SESSION ), uploaded files ( $_POST ), cookies ( $_COOKIE ), the server environment ( $_SERVER ), etc. Superglobal arrays are predefined variables that are always available in all scopes in any PHP script. This language construct makes accessing HTML form data simple. By specifying which PHP script will be executed and by which method the form data will be passed on when the submit button is pressed as attributes to the form tag in HTML, the called script can access the value of an element in the form by accessing $_GET[‘name’] or $_POST[‘name’] , where “name” is the name given to that form element as an HTML attribute. The GET method passes the

3 variables and their values along by appending them to the URL of the called script whereas the

POST method only stores the variables in the $_POST superglobal array. The latter method would be preferred in applications where private information, like login credentials, is being processed. In addition to accessing and processing form data, PHP provides support for interacting with a large number of database management systems, such as MySQL. A SELECT query to a table in a database returns an array that contains each tuple in that table that matches the query conditions. Accessing a field in that row is as simple as using the identifier

$row[‘field_name’] after the statement $row = mysql_fetch_array($query_result) is executed.

Evaluation

PHP is semantically similar to programming languages like Java and C and the scripting language Perl. If one is familiar with these languages, understanding a PHP program and learning the language is slightly easier. Some users of the language may be surprised by some of

PHP’s features after having used Java, such as its weak, dynamic typing for variables, its lack of return types in function signatures, and its ability to invoke functions in core PHP without having to explicitly include libraries or packages.

The writability of PHP is improved by not having to worry about associating data types with variables or including a library that includes a useful function like str_replace() . PHP is noticeably less verbose than Java. However, PHP’s readability suffers as a result. It can become difficult keeping track of which data type a variable refers to in a large script. Readers of

PHP source code must rely on an author’s comments or choice of variable names if the context in which a variable is used does not imply which type it refers to.

PHP is computationally expensive. The embedded source program is compiled on-the-fly by the PHP interpreter into a format that is then immediately executed by the PHP engine on the

Web server whenever the HTML document is requested. Programs that are interpreted like this usually execute ten to one hundred times slower than programs that are compiled.

The following is an example of a PHP program that is very similar to the example programs used throughout the second chapter in the tenth edition of Concepts of Programming

Languages :

<?php

/* PHP Example Program

* Input: An array, $list, that contains integer values.

* Output: The number of values in $list that are greater than

* the average of all values in $list.

*/ function foo($list){

$numItems = sizeof($list);

$sum = 0;

$result = 0;

// Compute the sum

for ($counter = 0; $counter < $numItems; $counter++){

$sum += $list[$counter];

}

// Compute the average

$average = $sum / $numItems;

// Count the input values that are > average

foreach ($list as $value){

if ($value > $average) $result++;

}

return $result;

}

// Test the foo function

$a = array(5,2,9,3,22,21,30,43,21,21,27,1,44,3); echo "There are ", foo($a), ' values > average in $a.<br/>';

?>

Output:

There are 8 values > average in $a.

Sources

http://us2.php.net/manual/en/history.php.php http://en.wikipedia.org/wiki/PHP http://php.net/usage.php http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html http://stackoverflow.com/questions/3582439/subtle-oop-differences-between-java-and-php

Textbook: Concepts of Programming Languages (Tenth Edition) by Robert W. Sebesta

Personal experience from working with PHP

4

Download