Chapter 1 - Introduction to PHP

By: Albert Ritzhaupt

 

 


Chapter Topics

What is PHP?

What we now refer to as PHP was initially PHP/FI, and was created by Rasmus Lerdorf in 1995, as a simple set of scripts for tracking accesses to his online resume (PHP History, 2005). He named this set of scripts 'Personal Home Page Tools’; hence, the acronym PHP (PHP History, 2005). Many people saw his website and asked for the tool via email. As the popularity of the tool grew, so did its functionality. Rasmus later wrote a much larger implementation written in the C programming language, which was able to communicate with databases, and enabled users to develop simple dynamic Web Pages (PHP History, 2005).

In July of 2004, PHP version 5 was released. However, it was PHP version 4 that penetrated the Web market as a robust application solution. “Today, PHP is being used by hundreds of thousands of software developers (estimated), and several million sites report as having it installed, which accounts for over 20% of the domains on the Internet (PHP History, 2005)." To provide more clarity, we break down a simple definition: “PHP is a server-side, cross-platform, HTML embedded scripting language that lets you create dynamic web pages (Terna, 2005)."

When dealing with static HTML pages, a client will provide a URL through a Web Browser, which requests an HTML file from a server connected to the Internet. This process is visualized in figure 1. PHP web pages are treated just like static HTML pages with the exception that PHP files are first executed by the web server before the information is sent to a client computer. Since the information is first processed on the server, the content that is delivered to the client is dynamic in that it will change based on the request; hence the phrase dynamic web pages. This description relates to the term server-side in the definition provided.

The term cross-platform means that PHP can run on a multitude of operating systems that have the necessary software modules to support it. Traditionally, PHP was used on UNIX/Linux based systems using the Apache web server. However, recent extensions to the Apache web server and the wide-spread acceptance of PHP as a serious web-based application solution has lead to developments on IIS (Microsoft) servers as well. PHP can run on any system that is running the Apache web server.

The phrase “HTML embedded scripting language" is of utmost importance. This essentially means that PHP can embedded with in HTML to create dynamic web pages. The word scripting means that PHP is an interpreted language as opposed to a compiled programming language. In this online book, we will explore the use of PHP to create dynamic websites.

back to top

What is programming?

“Computer programs tell the computer what to do—which information to identify and access, how to process it, and what equipment to use. Programs vary widely depending upon the type of information to be accessed or generated. For example, the instructions involved in updating financial records are very different from those required to duplicate conditions on board an aircraft for pilots training in a flight simulator (BLS, 2005).”

Web programming is very different than traditional computer programming, where the program and the data on which the program operates are on the same computer. Web programming involves many different data sources usually on many different computers. For instance, the client may provide data to a computer program on a web server, and based on the information provided, the program may access a database in a different country. The key to web programming is understanding that the information comes from many sources, but must be available to your program in order to be processed.

Generally, a client sends information via a web browser with an HTML form. This beckons a short discussion on POST versus GET. Since you already have some HTML skills, you should remember the form tag has the action and method attributes. For example, look at the following code:

<html>
<form action="file.php" method="POST or GET">
<input name="example">
<input type="submit" value="Submit">
</form>
</html>

This source code would create HTML that looks like the following:

When the POST method is chosen, the information is packaged and sent to the web server so that the user cannot see the information being sent. When the GET method is chosen, the information is appended to the URL in a process called “URL Encoding”.  Remember this information because it will be very important in the subsequent chapters. 

back to top

Syntax versus Logic Errors

It is important for you to understand the difference between a syntax error and a logic error.  A syntax error means that there is a violation in the rules of the language.  For instance, in the English language, when you present a list of items, you are supposed to separate each of the items with a comma.  Therefore, the sentence "My favorite colors are purple orange green and yellow" would be considered syntactically incorrect because it does not have commas separating each of the items.  An example that you might be familiar with is when you forget to place a closing tag in HTML.  With regards to PHP, if you have a syntax error, the web server will issue an error message telling you to fix your error.

A logic error works much differently.  A logic error is an error in the instructions of the program.  For instance, if a program is supposed to show the names of individuals that are female AND older than 23 from a list, and instead it shows the names of those that are female OR older than 23 from a list, it has a logic error.  When there is a logic error, the program will still run, but the output or purpose of the program is incorrect.  Consequently, logic errors cost organizations lots of money, so be very careful.  Just because it works does not mean it is correct.

Example Syntax Error

The figure above is an example of a syntax error.  The web server will inform you of these errors if they exist in your source code.  However, as you can see, the information provided by the web server is not clear as to the exact nature of the problem.  From the example, we can see that the error is reported to be on line number 10.  As a general strategy, the first place you should look kin your source code should be line number 10.  The error may, however, be the statement above line number 10.  The type of error that is reported here is a parse error, which generally means that you are missing a double quote to close a string or that you did not provide a semicolon at the end of a statement.

As you practice programming in PHP, you will become more familiar with the errors and they will eventually be easier to identify.  The code for the error can be seen below.  Clearly, the error is not on line number 10.  The string shown with the echo statement is missing a double quote.

<?php
echo "this is a test;


if($var)
{

}
?>

back to top

The Web Server: Apache

The Web server is the essential core to a Web site. It is, in essence, a software program that accepts requests from clients to resources (files) on a server. PHP is normally run on an Apache web server. The Apache web server, according to www.netcraft.com, a reliable Internet survey organization, is the most widely used web server in the world (Netcraft, 2005). “The Apache web server is the best, and most preferred, HTTP server software in use on the Internet today, and it was written entirely as a volunteer project, by volunteer programmers, in their spare time (Apache, 2005)." The Apache web server is an open-source license.

back to top

PHP and HTML

As previously mentioned, PHP can be embedded within HTML. It is important that you understand the difference between the purpose of PHP and HTML. PHP is used to dynamically deliver the information that is to be presented in HTML. The HTML itself only has to do with the presentation of the information. Any gathering or processing of information will be accomplished using PHP, whereas the presentation of the information is accomplished using HTML.

<html>
<h3>A PHP Program</h3>
<? echo "Hello world!"; ? >
</html>

The diagram above is an example of PHP embedded within HTML. Notice that the PHP source code is encapsulated with the <?php … ?> tag. When the web server receives a request for a specific PHP web page, the web server will first process any of the source code found within this tag, then deliver the resource to the client. This is what we mean by embedding PHP within HTML. Keep in mind, simply putting this information within a file is not sufficient. The subsequent sections will explain how to accomplish this followed by a step-by-step example.

back to top

File Extensions and Setting Permissions

Simply embedding PHP in HTML is not sufficient for the web server to recognize and execute a PHP file. Additionally, the programmer must ensure that the PHP file contains the correct file extensions (.php). Just as you have created a file with the extensions .html, .htm, or .txt, you can create a file with the .php extension.

Now that you know to set the correct file extension, we can discuss setting the correct UNIX/Linux permissions to execute a PHP file. You must set the file to read and execute for other, and of course you should always provide yourself with complete access to the file. The diagram below shows how to set the correct file permission using the chmod command. Notice that you provide yourself full-access (7), your user group no access (0), and other read and execute access (5).

prompt$: chmod 705 file.php

To provide a little review, remember that UNIX/Linux permissions allow you to set three different configurations for three different user groups. The user groups include the user (you), your group (most likely your peers), and other, which means the rest of the world. The three configurations for each user is read (4), write (2), and execute (1). Any combination of these three values can be assigned to any given user within the range of 0 to 0 + 1 + 2 + 4 = 7. The table below summarizes the permissions.

Configuration Value
Read 4
Write 2
Execute 1

back to top

Your First Program

This section will provide the steps for you to create your first PHP program.

  1. First create a file named "first.php" in a directory that is recognized by the web server. The file should contain the following information:

    <html>
    <h3>A PHP Program</h3>
    <?php
    echo "<h4 align='center'>";
    echo "Hello world!";
    echo "</h4>";

    ? >
    </html>


  2. After you have created the file and put the information into the file, save your work, and change the permissions of the file to:

    prompt$: chmod 705 first.php


  3. Open the URL to the location where you saved the file and you should have the following output:

    A PHP Program
    Hello World!


    Notice the echo statement simply outputs the information that was provided within the double quotes. Also, view the source of the PHP file in the web browser. Notice that the PHP source code is not visible. This is because the program was executed on the server and the tag was replaced with the output of the code... in this case "Hello World!".  Also, notice that you use a single quote (') to define the value of an HTML attribute.  If you tried to use a double quote, the web server would not know the difference between the end of the string or the beginning of the attribute value.  You could also use the backslash (\) character followed by the double quote (\"), which is a way to represent a special character in a string.

  4. Re-open the file, and change the information to the following:

    <html>
    <h3>A PHP Program</h3>
    <?php echo "Hello world!" ? >
    </html>


    In this example, you did not provide the semicolon at the end of the statement. Now refresh in the web browser, and you should receive a web application error. This error is because your program has a syntax error - it is missing the semicolon at the end of the statement. Change the file back to its original state and save.

Congratulations! You have completed the first chapter. You can now take the online assessment, complete the first activity, and move to the next chapter. 

>>Take the Online Quiz

back to top

Activity 1

Create a simple PHP program to print your full name with a red font using a 25 pitch font.  Your name should be centered and followed by your email address as an active link using mailto.  This could be done using just HTML, but instead use the echo statement to print out each of the items.

back to top

Chapter References

  • Introduction to the Apache Server, (2005). Retrieved on August 9, 5005 from: http://apache.rcbowen.com/ApacheServer.html#Introduction_to_the_Apache_Serve.
  • PHP History, (2005). Retrieved on August 9, 5005 from: http://us2.php.net/history.
  • Client Server Model, (2005). Retrieved on August 9, 5005 from: http://homepages.uel.ac.uk/u0315352/Web%20server.htm.
  • PHP Definition, (2005). Retrieved on August 9, 5005 from: www.terena.nl/library/gnrt/appendix/glossary.html.

back to top

 

Copyright 2005 Albert Dieter Ritzhaupt. All Rights Reserved.

back to top