Chapter
1 - Introduction to
PHP
|
|||||||||||||||||||||||||
|
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. 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:
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. Syntax versus Logic ErrorsIt 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. 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.
The Web Server: ApacheThe 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. PHP and HTMLAs 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.
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. File Extensions and Setting PermissionsSimply 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).
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.
Your First ProgramThis section will provide the steps for you to create your first PHP program.
Congratulations! You have completed the first chapter. You can now take the online assessment, complete the first activity, and move to the next chapter. Activity 1Create 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. Chapter References
|
||||||||||||||||||||||||
|
|||||||||||||||||||||||||