- Read the PHP Gentle Introduction article at communitymx.com http://www.communitymx.com/content/article.cfm?cid=D5084&print=true
- Answer the questions within each example exercise below
Foreach loop
- How are Foreach loops used?
- How do they work?
- Create and example where it loops through a $color_list array, assigning each element in turn to $item and displaying it with echo.
My example output looks like this:
red
orange
green
blue
yellow
While loop
- How do While loops work?
- Create and example where a condition remains true while the counter is less than or equal to 10. As soon as the counter reaches 11, the loop stops.
My example output looks like this:
1
2
3
4
5
6
7
8
9
10
For loop
- How do For loops work?
- Create and example where the counter ($i) is set to a, the condition is set to run the loop while the counter is less than or equal to j, and the counter is incremented by 1 each time the loop runs. Note: Figure out what to do to make the values show up on the same line as per my example.
My example output looks like this:
a, b, c, d, e, f, g, h, i, j,
Do... while loop
- How does a do... while loop work?
- Create an example where the counter ($i) is set to a number inside the loop, and increased by 50 each time the loop runs until the condition returns false at 500 then stops. Note: As per the previous example, figure out what to do to make the values show up on the same line as per my example.
50 100 150 200 250 300 350 400 450 500
Single vs. Double Quotes:
Create an example where you use both single and double quotes to have the correct output for the value of $name where $ name is processed in the double quote line and shown as treated as plain text in the single quote line as per my example:
Double quotes: The teacher's name is Jeffrey Diamond
Single quotes: The teacher's name is $name