Lab Sheet 3: PHP - First Steps

PHP is a scripting language that is especially suited for Web development and can be embedded into HTML. PHP is relatively easy to learn and is similar to other scripting/programming languages such as javascript and java though there are significant differences as well. This lab will focus on string processing in php as this will be essential in future work. We will also touch on arrays in php and randomizing techniques, and explore how these ideas can be combined to make use of the 10 x 10 data that was presented in class.

You might want to open a browser window to www.php.net when working in php. W3schools has a PHP tutorial that you may possibly find useful.

  1. Getting Started with Dreamweaver

    Any text editor can be used to create a modify a PHP document; testing is another matter: A web browser is not designed to process PHP code and testing must be done on a web server configured with PHP. This means that we must ftp our PHP file to the server before we can test the script out. Dreamweaver can be set up to automatically ftp a file to the server once things have been configured correctly. This can be real time saver.
    1. Set up an ftp site in Dreamweaver by selecting Site | Manage Sites, clicking on the new button, choosing the ftp option and using the form to fill in server and account information (FTP Host = http://susan.ns.purchase.edu, Host Directory = /home/johndoe/public_html). Deselect the save checkbox when working in the lab!
    2. To create a new document that will automatically be transferred to the server: Make sure the Files panel is open (View | Show Panels). Right-click (in windows) on the ftp:susan (or whatever you called the server) text in the Files panel and select new File. Name it helloworld.php
  2. Create and Test Your First PHP page:

    1. Type or paste into your php file the skeleton of an html page (<html>,<head>, </body> tags) along with some simple content a la "Hello World"
    2. Save and test by pointing a browser to http://susan.ns.purchase.edu/~johndoe/helloworld.php
    3. Add some dynamic content to your page by pasting
       
      <?php echo date('l dS \of F Y h:i:s A'); ?>
      
      somewhere in the body of the document. Save and test again.
  3. Strings and Variables in PHP:

    PHP doesn't use strict typing like more conventional programming language such as Java and C++. This makes things easier in the short run but more challenging for larger scale applications. It is still important to understand the distinction between strings and numbers on the one hand, and different kinds of numbers (integers vs. decimals).
    1. Read the PHP Syntax page at W3schools and test out the examples given there.
    2. Create a script that initializes two variables called protocol and domain, initializes them to http and www.purchase.edu resp., splices them together into the complete url for the Purchase homepage, and echoes the result to the output stream.
  4. Processing 10 x 10 Data:

    10 x 10, designed and developed by Jonathan Harris, is a data resource for developers and artists. Every hour, 10x10 gathers the 100 most important words and pictures in the world, based on what's happening in the news. The images and words that are collected are made available for non-commercial purposes.
    1. Read up on how the data can be accessed. We can include an image from the 10 x 10 archive in our page by using the appropriate url in conjunction with an HTML <img> tag. For example, inserting
       
      <img src="<?php echo 'http://tenbyten.org/Data/Now/now.jpg';?>">
      into your php file should add a single image of the 10x10 grid for the current hour into your page. Test it!. (We actually don't really need echo out the url in this instance since this is static text but this strategy will be useful in the following steps.)
    2. To do more interesting things with the 10 x 10 data we have to access the words.txt file for the date/time of interest. For example, the url for the current 100 most popular words is http://tenbyten.org/Data/Now/words.txt. We can read this list into an Array with the following php statement:
      $words = file("http://tenbyten.org/Data/Now/words.txt");
      
      Individual words can be accessed by using standard array syntax ($words[0] refers to the first word in the file, $words[1] to the second, etc..). Try this out and echo out the first 5 words in the list.
    3. Insert the current top-ranked image into your document: Use splicing techniques to put together the url of the current top-ranked image and insert the corresponding image into our document as above.
    4. Modify your script so that it displays a random image from the current top 100 list: The built-in PHP function array_rand will pick a random element from an array. For example, array_rand($words) returns a random word.
    5. Add a hyperlink so that when the user clicks on the image we are taken to the Technorati tags page for the word that accompanies the random image.
  5. Uploading Work to Personal Web Page:

    Upload your work in this lab to your personal page.