Review: databases and PHP
Now Unit 3. Read the code slowly and write the answer before looking at it. In double quotes PHP puts the value of a variable into the text, and in single quotes it prints the characters as they are. An if with elseif and else tries the conditions from the top and runs only the first one that is true. A switch jumps to the matching case and then keeps running the next cases until it meets break; the word default runs when nothing matched. The classic mistakes are: a missing semicolon, one = (put a value) where == (compare) was meant, a forgotten $ before a variable name, and a closing tag that was never opened. For a form, choose post to send data that will be saved, such as a new term or a file, and get for a request that only looks something up. An html page uses lowercase tags and its picture needs an alt text.
1<?php 2$club = "Readers"; 3echo "Welcome to $club", ' $club'; 4?>
Welcome to Readers $club
1<?php 2$books = 5; 3if ($books >= 10) { 4 echo "gold"; 5} elseif ($books >= 3) { 6 echo "silver"; 7} else { 8 echo "new"; 9} 10?>
silver
1<?php 2$d = 2; 3switch ($d) { 4 case 1: echo "A"; 5 case 2: echo "B"; 6 case 3: echo "C"; break; 7 default: echo "D"; 8} 9?>
BC
1<h1>Reading Club</h1> 2<p>Open on <b>Saturday</b>.</p> 3<a href="help.php">Help</a>
Find the invalid line in each of these four PHP lines and write the correct line: (a) echo "Hello" (b) if ($a = 3) { echo "x"; } meant to compare (c) echo name; meant to print the variable $name (d) a closing </p> without an opening <p>. Then choose get or post for a search box and for the Add Term form, and give a reason.