Intro to PHP: Iteration Lab

Iteration Lab

Key Words:function, method, procedure, form control, input, id, onchange, event handler, function call, function definition, function head, formal parameter, function body, variable, variable declaration, assignment operator, assign, value, text, string of characters, +, string concatenation, string.
See Wikipedia (http://en.wikipedia.org/) for a detailed description of the key words.

Preparation

You are required to have completed the objectives in theViking Lab before doing this lab.

Objectives

Remember to use the online resources! When you encounter a new HTML element, look at W3Schools XHTML tutorial and W3C's XHTML and HTML recommendations. When you encounter a PHP function or want to find what you can do with an HTML element in your PHP code, look at W3Schools PHP tutorial. You do not need to understand everything in their tutorials, but you should at least know where to find help.

If-Else Statements

The goal of this section of the lab is to practice writing if/else statements.

  1. Create a new PHP document for this lab. Save this into your php directory.
  2. Add to the <BODY> element of your page by initializing the variable count in the HTML file. Print the variable to the screen and view the results.
    <?php
    $count = 1;
    print('The count is: '. $count);
    ?>
    
  3. Now add some code that tests the value of count before alerting the user.
    <?php 
    $count = 1;
    $ready = false;
    
    if ($count == 0) {
      print("I'm not ready!<br />");
    } else {
      $ready = true;
      $count = $count-1;
    }
    
    echo("Ready: " . $ready . ", Count: " . $count ."<br />");
    ?>
    
  4. Change the value of count to 0. Reload the page.
  5. Allow the user to input the count variable using a text input field.
    You will have to change the variable initialization and assignment for the count variable.
    if (isset($_GET['count'])
      $count = $_GET['count'];
    } else {
      $count = 0;
    }
    
    Next add the input field to the HTML document.
    <form action="<?php $_SERVER['PHP_SELF']" method="get">
    Enter a number: <input id="count" type="text" />
    <input type="submit" value="Submit" />
    </form>
    
  6. Change the math in the if statement so that when the count is greater than 10, ready will be false.

Program Basic Iterations

The goal of this section of the lab is to practice writing iteration statements and become familiar with how the for-statement works. We will also place images, which should be helpful on Project 2.

  1. Start by initializing the variable value in your document. Next initialize and assign the variable i.
    <?php
    $value = 1;
    for ($i=0; i<$value; $i++) {
    	echo "Print this text<br />";
    }
    ?>
    
  2. Declare an iteration variable (most programmers will pick i and that is what the examples assume). Write a loop that iterates 5 times, repeating the statement:

    print("The iteration variable has the value " . $i . "<br />");

    If the loop’s initialization sets i to 0 and limits the iteration to numbers less than 5 and increments by 1 each time, then the result should be

    basic iteration
  3. Change the loop’s test to be i < 10. Show the result.
  4. Change the field of the for-statement to increment by 2. Show the result.
  5. Change the for-statement to count down from 10 to 1 by 1. The calculation must subtract 1 on each cycle. Show the result.
  6. Move the following image from this lab page to your desktop.
    star
  7. Revise your iteration so that it just loops five times. (It doesn’t matter where you start, stop or the amount you change the iteration variable by, as long as the loop iterates only five times. Make the following statement be the statement that is repeated:
    print("<img src='star.jpg' />< br />");
    
    The result should look like the following

    stars

HTML References

http://www.w3schools.com/js/default.asp
http://validator.w3.org
http://www.w3schools.org/
http://www.webmonkey.com/