Enhancing Websites with PHP

Introduction to Servers

PHP is a popular server-side scripting language used for developing dynamic websites. The acronym is a recursive abbreviation of Hypertext Preprocessor, meaning an application developed with PHP takes hypertext input from an HTML web form, processes the input by following the instructions contained in the PHP script, and then outputs the results back as HTML. Input and output are taken from and delivered to client systems via the browser while processing takes place on a web server equipped with the PHP interpreter.

Since processing occurs on a server, it is necessary to install a server environment on your personal (client) computer in order for you to develop web applications with PHP. Popular open source server packages are available that include the Apache HTTP Server™, MySQL™ database server, and the PHP interpreter. Which version of the package you need to install depends on your operating system. Options are WAMP for Windows® and MAMP for Mac OS®. If you have a Linux® operating system, the server environment is already included. If you use Windows, you also need to know if the version you have is 32- or 64-bit. To find out, open the Control Panel and click System.

Download sites are as follows:

WAMP: http://www.wampserver.com/en/
MAMP: http://www.mamp.info/en/index.html

Before installing the package, consider if there are other services running on your computer that might use the same communication port that your web servers need. Web servers communicate with clients via the HTTP protocol on port 80 on Windows and port 8888 on Mac OS and it is necessary to either temporarily stop any services using those ports or change the ports to avoid conflicts. Common services known to cause a conflict on Windows include Microsoft® Internet Information Services® (IIS) and Microsoft® SQL Server®. Services that could cause conflicts on both Windows and Mac OS include bit torrent applications and internet phone applications, such as Skype™. On a Windows system, you can temporarily stop a service in the Services console located in Administrative Tools. Mac users should temporarily disable web sharing in System Preferences if the port used by the service is 8888.

Server Installation and Operation

Installation is quick and easy. Simply follow the onscreen prompts accepting all default settings. Once installed, you will see a new folder on your drive, typically c:\wamp on Windows or /applications/mamp on Mac. In those folders are several subfolders. The subfolder you will work with the most on Windows is c:\wamp\www or /applications/mamp/htdocs on Mac. The www and htdocs folders are the web root folders accessible by the browser. You will create a subfolder for each unit project in the c:\wamp\www or /applications/mamp/htdocs folder and save your project files in the respective unit folder. Project files will include HTML and PHP files and may include images and cascading style sheets (CSS) depending on your creativity.

You launch the servers like any other application. On Windows, what is different from other applications is the servers do not "open" in a window; rather an icon appears in the system notification area indicating the servers are ready and waiting for you to request a web page from your www folder using your browser. Mac users will need to click the "Start Servers" button in the program window that appears to ready the servers for use.

You request a web page from your localhost in the same way you do when requesting one from a remote server by entering a uniform resource locator (URL) in your browser address bar.

The URL for your server on Windows is as follows.

http://localhost/
-or-

http://127.0.0.1/

The URL for your server on Mac is as follows:

http://localhost:8888/
-or-

http://127.0.0.1:8888/

On Windows, you can alternatively open the browser at the root URL shown above by clicking the server icon in the system notification area and selecting Localhost from the menu. To open a specific project, scroll to the section called Your Projects on the home page that appears and select the project you want to test.

Testing Scripts

Knowing how to test your projects is essential to mastering how to write functional PHP scripts. Use the following check list to test every project to ensure your server environment is working properly. Doing so will help you isolate and fix errors and will help build your confidence as you gradually learn the language. If you follow a systematic approach and make time for focused study and practice, you will realize you are beginning to understand what you are writing and what the code is doing. Experiencing such "light bulb" moments is exciting and highly motivating even to seasoned programmers!

Testing Checklist

&#9744 Launch the servers.
&#9744 Start servers (Mac only).

*Note: On Windows, the servers do not need to be "online". That is only for environments that allow remote users to access the servers.

&#9744 Write your code using a text editor for coders.
&#9744 Save your code file(s) in the applicable unit project folder.
&#9744 Launch the browser at the applicable URL:

Windows: http://localhost/unitt#/filename.php
Mac: http://localhost:8888/unit#/filename.php

&#9744 Make note of any errors including the type of error and line number. Because PHP is interpreted from left to right and top to bottom, a syntax error will exist on the line referenced or on a line above that line number.
&#9744 Isolate and fix errors one by one retesting each time until the script runs error-free.
*Note: The closer the line number is to the last line in the script, the closer you are to having a fully functional script.

Lesson Summary

In this unit, you learned what PHP is and how PHP works. In addition, you were introduced to the basic syntax of the language and its fundamental programming concepts including variables, constants, and common data types including strings and numbers. You should have a good understanding of the difference between using single and double quotes, when you need to escape a character, and the basic debugging steps that help you fix errors. If any of this is not clear, please review the reading prior to proceeding. Specifically, at this point, you should be able to:

  • Install the WAMP (Windows) or MAMP (Mac) server package.
  • Create a simple script that defines and displays three constants: your name, the course and unit number, and the date you completed the script.
  • Run the script on the web server

Assignment

For this project, you need to install the server package applicable to your operating system. Once you have done that, you will write a simple script using the techniques you have learned and run it on your server via the browser.

Instructions

Perform the following steps:

  1. Install the WAMP (Windows) or MAMP (Mac) server package. Accept all default settings.
  2. Launch the servers and make sure they are turned on.
  3. Start a new file in your editor and save the file in the following location. Make sure you create the unit01 subfolder in your server root folder and that the file name has .php as the extension.

    Windows: wamp/www/unit01/unit01.php
    Mac: /applications/mamp/htdocs/unit01/unit01.php
    NOTE: Dreamweaver users should click File > New and choose PHP from the Page Type list to automatically insert HTML document structure tags. Users of other editors will need to manually enter HTML document structure tags.

  4. Insert a title of Your Name IT250 Unit 1 between the opening and closing title tags. Example (replace John Doe with your actual name):
    <title>John Doe IT250 Unit 1 </title>
  5. Within the body section of your page, type the opening and closing PHP delimiters: <?php   ?>
  6. Define three constants as follows:

    a. Your actual first and last name;
    b. The course and unit number;
    c. The date you completed the project.

  7. Display the value of each constant on separate lines using the echo function.

    HINT: Concatenate a
    tag as a string just before the semi-colon at the end of each echo statement.

  8. Optional but encouraged: Format your page layout and content using what you previously learned. Suggestions include formatting the lines as headings, customizing font colors, centering the output in the browser, inserting an image using the tag, and applying a background color or tiled image. You may also want to include additional content to make it more interesting! You are limited only by your own imagination!
  9. Save your changes.
  10. Open your browser to http://localhost/unit01/unit01.php and admire your work!

    TIP: Download and use the Test Your Project Check List from Document Sharing to be sure you didn’t overlook anything. In the event your page does not display as expected, refer to the section entitled "Basic Debugging Steps" in your text on pp. 32-33.

Quiz