RU beehive logo ITEC dept promo banner
ITEC 325
2011fall
ibarland

homelectsexamshws
breeze (snow day)

php-ch02
ch02: php types

From PHP Visual Quickstart Guide by Larry Ullman
Originally based on notes by Jack Davis (jcdavis@radford.edu)


Intermingling php and raw-output

We previously saw
 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  ?>
However, with php's raison d'etre of making text-processing (and html-generation) easy, we can actually have things not between php processing-instrctions <?php ?$gt;; this is treated as literal output:
 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>

1      

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 $x = false || true; is find; it means So $x = (false || true);, and $x gets the value true. But $x = false OR true; means ($x = false) OR true;, which means $x gets false but the entire line returns true if used in some bigger context. (Btw, using the result of assignment in a bigger context is rarely a good idea; it's a holdover from more primitive I/O libraries that didn't have any peek/hasNext functionality.)
Bottom line: use && and ||, and use parentheses if you need some unusual precedence.      

homelectsexamshws
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
Powered by PLT Scheme