|
home—lects—exams—hws
breeze (snow day)
From PHP Visual Quickstart Guide by Larry Ullman
Originally based on notes by Jack Davis (jcdavis@radford.edu)
By default, using an uninitialized/undeclared variable is not an error!
(This is unfortunate, since it means misspelling a variable-name
turns into a hard-to-track bug.)
We'll see later how to configure php (or a web server using php)
to emit a warning when using uninitialized variables;
you want to always have that as a default.
$str = "How are you?"; $first_name = "John"; $str = "How are you " . $first_name . "?"; $str = "How are you $first_name?"; // alternately $date = "1/01/2010"; // this is a string! $str2 = "I said, \"how are you?\""; |
if (!$fname) { // We reach here if $fname was undefined *or* the empty string. echo "No first name provided."; … } |
$dbConnection = |
$dbConnection = |
$arr = array("john","mary","zimbo"); echo "index 1 contains ", $arr[1], "\n"; // "mary" $arr[] = "betty"; // Insert at the *next* location. echo "index 3 contains ", $arr[3], "\n"; // "betty" $arr[23] = "justine"; echo "index 17 contains ", $arr[17], "\n"; // NULL echo "index 23 contains ", $arr[23], "\n"; // "justine" $arr[] = "rachel"; // Wait, what counts as the *next* location now? 24, not 4 (!). echo "index 4 contains ", $arr[ 4], "\n"; // NULL echo "index 24 contains ", $arr[24], "\n"; // "rachel" |
$crsarr = array("web"=>325, "java1"=>120); |
print_r( $crsarr ); print_r( array(20, 21, "howdy" => "bye", "aloha" => "sayonara", 2 => 22 ) ); |
define ("PI",3.14152926535); echo "The constant PI holds ", PI, "<br/>\n"; |
number_format(428.4959,2) == '428.50' number_format(428,2) == '428.00' number_format(123456789) == '123,456,789' |
1 <?php 2 3 // A flag to turn the test-cases on/off. 4 define('RUN_TESTS',false); 5 6 7 /** Return a blend of two words: 8 * @param $word1 The word to take the first half from. 9 * @param $word2The word to take the first half from. 10 * @return the first half of $word1 concatenated to the second half of $word2. 11 * (If a word is odd length, we round the halfway-point down.) 12 */ 13 function blend( $word1, $word2 ) { 14 $fHalf = substr($word1, 0, strlen($word1)/2 ); 15 $lHalf = substr( $word2, strlen($word2)/2 ); 16 return $fHalf . $lHalf; 17 } 18 19 20 /** Check whether the actual and expected values are the same. 21 * If not, print an error message. 22 * @param $actual The actual result of the function-call. 23 * @param $expected The expected (desired) result of the function-call. 24 */ 25 function test($actual,$expected) { 26 if ($actual === $expected) { 27 echo "."; 28 } 29 else { 30 echo "Expected \"", $expected, "\",\n"; 31 echo "but got \"", $actual, "\".\n"; 32 } 33 } 34 35 36 if (RUN_TESTS) { 37 test( blend("a", ""), "" ); 38 test( blend("", "a"), "a" ); 39 test( blend("", "abc"), "bc" ); 40 test( blend("abc", ""), "a" ); 41 test( blend("", ""), "" ); 42 test( blend("a", "1"), "1" ); 43 test( blend("ab", "12"), "a2" ); 44 test( blend("motor", "hotel"), "motel" ); 45 test( blend("smoke", "fog"), "smog" ); 46 test( blend("Ian", "Barland"), "Iland" ); 47 } 48 49 50 // Produce a valid web page: 51 52 echo "<html>"; 53 echo " <head><title>Rawr</title></head>"; 54 echo " <body>"; 55 echo " Hi Ian Barland -- I shall call you ", blend("Ian","Barland"), "\n"; 56 echo " (How do you like that?)"; 57 echo " </body>"; 58 echo "</html>"; 59 ?> |
1 <html> 2 <head> 3 <title>Rawr</title> 4 <?php 5 6 // A flag to turn the test-cases on/off. 7 define('RUN_TESTS',false); 8 9 10 /** Return a blend of two words: 11 * @param $word1 The word to take the first half from. 12 * @param $word2The word to take the first half from. 13 * @return the first half of $word1 concatenated to the second half of $word2. 14 * (If a word is odd length, we round the halfway-point down.) 15 */ 16 function blend( $word1, $word2 ) { 17 $fHalf = substr($word1, 0, strlen($word1)/2 ); 18 $lHalf = substr( $word2, strlen($word2)/2 ); 19 return $fHalf . $lHalf; 20 } 21 22 23 /** Check whether the actual and expected values are the same. 24 * If not, print an error message. 25 * @param $actual The actual result of the function-call. 26 * @param $expected The expected (desired) result of the function-call. 27 */ 28 function test($actual,$expected) { 29 if ($actual === $expected) { 30 echo "."; 31 } 32 else { 33 echo "Expected \"", $expected, "\",\n"; 34 echo "but got \"", $actual, "\".\n"; 35 } 36 } 37 38 39 if (RUN_TESTS) { 40 test( blend("a", ""), "" ); 41 test( blend("", "a"), "a" ); 42 test( blend("", "abc"), "bc" ); 43 test( blend("abc", ""), "a" ); 44 test( blend("", ""), "" ); 45 test( blend("a", "1"), "1" ); 46 test( blend("ab", "12"), "a2" ); 47 test( blend("motor", "hotel"), "motel" ); 48 test( blend("smoke", "fog"), "smog" ); 49 test( blend("Ian", "Barland"), "Iland" ); 50 } 51 ?> 52 </head> 53 54 55 <body> 56 Hi Ian Barland -- I shall call you <?php echo blend("Ian","Barland") ?>. 57 (How do you like that?)"; 58 </body> 59 60 </html> |
2 Well, “just like in other Algol-based syntaxes.” ↩
3 Well, “reasonably fully parenthesize” — the precedence rules are designed to make operators like function call, array-lookup, field-lookup etc. work as expected. Precedence between arithmetic is not really the purpose. ↩
4
So
Bottom line: use
home—lects—exams—hws
breeze (snow day)
©2011, Ian Barland, Radford University Last modified 2011.Sep.07 (Wed) |
Please mail any suggestions (incl. typos, broken links) to ibarlandradford.edu |