PHP Programming Fall 2013
Class Exercise: Control Structures I
1. If statement:
Test whether the variable $orderQty is equal to zero, and when true print
the message "order quantity zero!".
2. If-else statement:
Given that form data for 'orderQty' is "posted" to a PHP program, code an
if statement that tests whether 'orderQty' is missing or empty and when
true assigns zero(0) to $orderQty, otherwise it assigns the value passed as
'orderQty' to $orderQty and prints "order quantity = $orderQty".
3. While loop:
Construct a while loop with a statement block to input and print data from
a file referenced by the pointer $fp with input data assigned to $dataIn. Have
a "primer read" precede this loop. The loop should continue as long as the
function feof($fp) returns false. In the loop body, print $dataIn, then
input the next line of data. Revise this code to produce the same results
without needing a "primer read".
4. For loop:
Construct a for loop to print the squares of the numbers from 1 to 5.
5. Nested for loops:
Use nested for loops to print a multiplication table for the digits from
1 to 5.
6. Foreach loop:
Given that an fgetcsv function has been invoked to input a set of comma-
separated values to be assigned to an array named $inputItems, prepare
a foreach structure to iterate through $inputItems displaying each element
value.