Posts tagged Web Programming
PHP #6: Even More Control Structures
Nov 27th
So far we have learned about the following control structures:
- if, elseif, else
- while
- for
But there are still more. We’ll first look at the switch construct.
Switch
The syntax for a switch statement looks something like this:
1 2 3 4 5 6 7 8 9 10 | switch (expression) { case value1: //some code case value2: //some code case value3: //some code default: //some code } |
When this code is executed, each case is looked at in order. If(value1 == expression), then all code under that case and the code under all subsequent cases will be executed. If not, it will check if(value2 == expressions) and so on. If none of the cases match, the default case is used. You can have as many or as few cases as you want, and having the default case is optional. An important thing that is often forgotten is that all code underneath a true case is executed, even the code within the following cases.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | $n = 3; switch($n){ case 0: print "$n is equal to 0\n"; case 1: print "$n is less than or equal to 1\n"; case 2: print "$n is less than or equal to 2\n"; case 3: print "$n is less than or equal to 3.\n"; case 4: print "$n is less than or equal to 4.\n"; case 5: print "$n is less than or equal to 5.\n"; } |
When the above code is executed, the following will be printed:
3 is less than or equal to 3. 3 is less than or equal to 4. 3 is less than or equal to 5.
Notice that once an appropriate case is found, all code in the following cases is executed as well. There is a way to prevent this though. Using the break; command you can jump to the closing brace of the switch statement preventing any more code from being executed.
Also, a default case will be used when no other cases match.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | switch ($n) { case 0: print "$n is zero."; break; case 1: case 4: case 9: print "$n is a perfect square."; break; case 2: print "$n is an even number."; case 3: case 5: case 7: print "$n is a prime number."; break; case 6: case 8: print "$n is an even number."; break; default: print "Only single-digit numbers are allowed."; break; } |
Test for Understanding
- What will print if $n is set to 7?
- What will print if $n is set to 1?
- What will print if $n is set to 20?
Answers:
- 7 is a prime number.
- 1 is a perfect square.
- Only single-digit numbers are allowed.
HTML 4/XHTML 1.0 Tag List
Jun 21st
DTD referrs to the doc type and indicated which tags are allowed F = Frameset, S = Strict T = Transitional.
| Tag | Description | DTD | XHTML 1.1 |
|---|---|---|---|
| <!–…–> | Comment in HTML | FST | YES |
| <!DOCTYPE> | Sets document type | FST | YES |
| <a> | Sets an anchor | FST | YES |
| <abbr> | Sets an abbreviation | FST | YES |
| <acronym> | Sets an acronym | FST | YES |
| <addres> | Sets contact information for the author/owner of a document | FST | YES |
| <applet> | Deprecated: Sets an embedded applet | FT | NO |
| <area /> | Sets an area inside an image-map | FST | NO |
| <b> | Sets bold text | FST | YES |
| <base /> | Sets a default address or default target for all links on a page | FST | YES |
| <basefont /> | Deprecated: Sets a default font, color, or size for the text in a page | FT | NO |
| <bdo> | Sets text direction | FST | NO |
| <big> | Sets big text | FST | YES |
| <blockquote> | Sets a long quotation | FST | YES |
| <body> | Sets a document’s body | FST | YES |
| <br /> | Sets a single-line break | FST | YES |
| <button> | Sets a push button | FST | YES |
| <caption> | Sets a table caption | FST | YES |
| <center> | Deprecated: Sets center text | FT | NO |
| <cite> | Sets a citation | FST | YES |
| <code> | Sets computer code text | FST | YES |
| <col /> | Sets attribute values for one or more columns in a table | FST | NO |
| <colgroup> | Sets a group of columns in a table for formatting | FST | NO |
| <dd> | Sets a description of a term in a definition list | FST | YES |
| <del> | Sets deleted text | FST | NO |
| <dfn> | Sets a definition term | FST | YES |
| <dir> | Deprecated: Sets a directory list | FT | NO |
| <div> | Sets a block-level section of a document | FST | YES |
| <dl> | Sets a definition list | FST | YES |
| <dt> | Sets a definition term (or item) | FST | YES |
| <em> | Sets emphasized (or italic) text | FST | YES |
| <fieldset> | Sets a border around elements in a form | FST | YES |
| <font> | Deprecated: Sets font, size, and color for text | FT | NO |
| <form> | Sets a form for user input | FST | YES |
| <frame /> | Sets a window in a frameset | F | NO |
| <frameset> | Sets a set of frames | F | NO |
| <h1> to <h6> | Sets different headings | FST | YES |
| <head> | Sets the head section of an HTML document | FST | YES |
| <hr /> | Sets a horizontal line | FST | YES |
| <html> | Sets an HTML document | FST | YES |
| <i> | Sets italic (or emphasized) text | FST | YES |
| <iframe> | Sets an inline frame | FT | NO |
| <img /> | Sets an image | FST | YES |
| <input /> | Sets an input field in a form | FST | YES |
| <ins> | Sets inserted text | FST | NO |
| <isindex> | Deprecated: Sets a searchable index related to a document | FT | NO |
| <kbd> | Sets keyboard text | FST | YES |
| <label> | Sets a label for an input element | FST | YES |
| <legend> | Sets a legend for a fieldset | FST | YES |
| <li> | Sets a list item | FST | YES |
| <link /> | Sets the relationship between a document and an external resource | FST | YES |
| <map> | Sets an image-map | FST | NO |
| <menu> | Deprecated: Sets a menu list | FT | NO |
| <meta /> | Sets metadata about the document | FST | YES |
| <noframes> | Sets alternative content for users who do not support frames | FT | NO |
| <noscript> | Sets alternative content for users who do not support client-side scripts e.g. JavaScript | FST | YES |
| <object> | Sets an embedded object | FST | YES |
| <ol> | Sets an ordered list | FST | YES |
| <optgroup> | Sets a group of related options in a form select list | FST | YES |
| <option> | Sets an option in a form select list | FST | YES |
| <p> | Sets a paragraph | FST | YES |
| <param /> | Sets a parameter for an object | FST | YES |
| <pre> | Sets preformatted text | FST | YES |
| <q> | Sets a short quotation | FST | YES |
| <s> | Deprecated: Sets strikethrough text | FT | NO |
| <samp> | Sets sample computer code | FST | YES |
| <script> | Sets a script type | FST | YES |
| <select> | Sets a drop down list | FST | YES |
| <small> | Sets small text | FST | YES |
| <span> | Sets a inline-section of a document | FST | YES |
| <strike> | Deprecated: Sets strikethrough text | FT | NO |
| <strong> | Sets strong (or bold) text | FST | YES |
| <style> | Sets an internal style sheet | FST | YES |
| <sub> | Sets subscript text | FST | YES |
| <sup> | Sets superscript text | FST | YES |
| <table> | Sets a table | FST | YES |
| <tbody> | Groups a table body | FST | NO |
| <td> | Sets a table cell | FST | YES |
| <textarea> | Sets a textarea for an input field | FST | YES |
| <tfoot> | Groups a table footer area | FST | NO |
| <th> | Sets a table header cell | FST | YES |
| <thead> | Groups a table header area | FST | NO |
| <title> | Sets the title of the document | FST | YES |
| <tr> | Sets a table row | FST | YES |
| <tt> | Sets teletype text | FST | YES |
| <u> | Deprecated: Sets underlined text | FT | NO |
| <ul> | Sets an unorderd list | FST | YES |
| <var> | Sets a variable part of a text | FST | YES |
| <xmp> | Deprecated: Sets preformatted text | None | NO |
PHP #5: More Loops
Apr 19th
There are a few other types of loops in addition to while loops that we learned about in Part 4.
Do-While Loop
A do-while loop is very similar to a while loop. The only difference is that the condition is evaluated after each loop iteration, rather than before. The syntax is like this:
1 2 3 | do { // some code here } while (condition) |
Since the condition isn’t evaluated until after the loop body, the loop body is always executed at least once, whether or not the condition is true. Once it does get to the condition, if it is true, it goes back up to the top of the loop body. If it is false, the loop terminates.
1 2 3 4 5 | $i = 11; do { print "The number is $i<br />"; ++$i; } while ($i <= 10) |
The above code prints:
The number is 11<br />
even though the condition is not true. The loop body always executes once.
For Loop
The for loop is a bit more complicated. The syntax looks like this:
1 2 3 | for (initial-expression; condition; loop-end-expression) { //some code goes here } |
The initial-expression is evaluated only once, at the very beginning of the loop. The condition is then evaluated. If the condition is true, the body of the loop executes. Then the loop-end-expression is run. The process then starts over again with evaluating the condition. A for loop is generally used to do simple bounding loops, like this:
1 2 3 | for ($i = 1; $i <= 10; ++$i) { print "The number is $i<br />" } |
As you’ll notice, the same exact thing can be done with a while loop, but many prefer the for loop as it is more compact:
1 2 3 4 5 | $i = 1; while ($i <= 10) { print "The number is $i<br />"; ++$i; } |
More Looping Examples
Let’s try using a for loops to create a multiplication table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | $start = 1; $end = 10; print '<table border="1" cellspacing="0" cellpadding="5">'; for ($y = $start - 1; $y <= $end; ++$y) { print "<tr>"; for ($x = $start - 1; $x <= $end; ++$x) { if ($y == $start - 1) { print "<td><b>$x</b></td>"; } else if ($x == $start - 1) { print "<td><b>$y</b></td>"; } else { print "<td>" print $x*$y; print "</td>"; } } print "</tr>"; } print "</table>"; |
Notice that there is a for loop inside of another for loop. These are called nested for loops. The outer loop loops through each row, and while the inner loop loops through each column. This means on every iteration of the outer loop, the entire inner loop runs. Trace the above code carefully to see how it works. This code will produce the following table:
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 1 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 2 | 2 | 4 | 6 | 8 | 10 | 12 | 14 | 16 | 18 | 20 |
| 3 | 3 | 6 | 9 | 12 | 15 | 18 | 21 | 24 | 27 | 30 |
| 4 | 4 | 8 | 12 | 16 | 20 | 24 | 28 | 32 | 36 | 40 |
| 5 | 5 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 |
| 6 | 6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 | 60 |
| 7 | 7 | 14 | 21 | 28 | 35 | 42 | 49 | 56 | 63 | 70 |
| 8 | 8 | 16 | 24 | 32 | 40 | 48 | 56 | 64 | 72 | 80 |
| 9 | 9 | 18 | 27 | 36 | 45 | 54 | 63 | 72 | 81 | 90 |
| 10 | 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 | 90 | 100 |
Test for Understanding
- In the code above how many times does line 6 run?
- In the code above how many times does line 8 run?
Answers:
- Line 6 runs while $y <= 10. $y starts at 0 ($start - 1) and goes up by 1 each time. So it runs while $start = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. That's 11 times!
- Line 8 runs on every iteration of the inner loop. The inner loop runs while $x <= 10. $x starts at 0 and goes up by 1 each time. So the inner loop runs 11 iterations. But the inner loop happens again on every iteration of the outer loop, which also iterates 11 times. 11 x 11 = 121 times.
