PHP

Some "self documenting" code to describe the PHP language

  1. <?php
  2. // This line must be before anything is sent to the browser (e.g. no html)
  3. ?>
  4. <html>
  5. <head>
  6. <title>PHP Examples</title>
  7. </head>
  8. <body>
  9. <?php
  10.  
  11. // Tell PHP to display errors:
  12. ini_set('display_errors', 1);
  13.  
  14. // A basic print statement
  15. print('<p>This is a sentence.</p>');
  16.  
  17. /* Another comment. Print all global variables */
  18. print_r($GLOBALS);
  19.  
  20. // Variables - they're case sensitive
  21. $var1 = 'Some text';
  22. $var2 = "Additional text";
  23. $var3 = 98;
  24. $var3++;
  25. $var4 = 1.1;
  26.  
  27. // Single vs double quotes in a printout:
  28. print("<p>var1: $var1</br>var2: $var2<br/>var3: $var3<br/>var4:$var4 </p>");
  29. print('<p>var1: $var1</br>var2: $var2<br/>var3: $var3<br/>var4:$var4 </p>');
  30.  
  31. // Basic math
  32. $total = ($var3 + 10) * $var4 / 2;
  33. $rounded_total = round($total);
  34. print("<p>The total is: $total, which rounds to: $rounded_total</p>");
  35. $new_total = number_format($total * 1000.1111, 2);
  36. print("<p>Multiplied by 1,000.1111 and formatted to two decimal places: $new_total</p>");
  37.  
  38. $rand_num = rand(0, 100);
  39. print("<p>A random number between 0 and 100: $rand_num </p>");
  40.  
  41. // String manipulation
  42. $concat_string = $var1 . ' ' . $var2;
  43. $same_string = "$var1 $var2";
  44. $safe_string = strip_tags('<script>alert(5);</script>');
  45. print("<p>Concat string: $concat_string. Same string: $same_string. Safe string: $safe_string.</p>");
  46.  
  47. $encoded_string = urlencode("This is encoded");
  48. $decoded_string = urldecode($encoded_string);
  49. print("<p>Encoded string: $encoded_string. Decoded string: $decoded_string.</p>");
  50.  
  51.  
  52. $full_string = 'Now is the time!';
  53. $full_string_len = strlen($full_string);
  54. $updated_string = str_replace('is', "ain't", $full_string);
  55. print("<p>Full string: $full_string. (Length: $full_string_len) Updated string: $updated_string</p>");
  56.  
  57. $st_tokens = strtok($full_string, ' ');
  58. $st_sub = substr($full_string, 1, 5);
  59. print("<p>st_tokens = $st_tokens. The substring (1-5): $st_sub.</p>");
  60.  
  61. // One way hash:
  62. $crypt_pwd = crypt('password');
  63. print("<p>password through crypt: $crypt_pwd</p>");
  64.  
  65.  
  66. // Control structures
  67.  
  68. // If-then-else
  69. $my_name = "Kenny"; // "Johnnie"
  70. if ($my_name == 'Johnnie') {
  71. print("<p>My name is Johnnie</p>");
  72. } else if ($my_name == 'Joey') {
  73. print("<p>My name is Joey</p>");
  74. } else {
  75. print("<p>I don't know my name</p>");
  76. }
  77.  
  78. $my_name= 7;
  79. if (is_numeric($my_name)) {
  80. print("<p>I feel like a number</p>");
  81. } else {
  82. print("<p>Fully human!</p>");
  83. }
  84.  
  85. // Switch statement
  86. $my_name = "Teddy";
  87. switch ($my_name) {
  88. case "Todd":
  89. print("<p>My name is Todd</p>");
  90. break;
  91. case "Teddy":
  92. print("<p>My name is Teddy</p>");
  93. break;
  94. default:
  95. print("<p>I still don't know my name</p>");
  96. break;
  97. }
  98.  
  99. // For loop
  100. print("<p>");
  101. for($counting = 1; $counting < 5; $counting++) {
  102. print("I'm counting from 1 to 4. This is number $counting. ");
  103. }
  104. print("<b>done</b></p>");
  105.  
  106. // While loop
  107. $counting = 4;
  108. print("<p>");
  109. while($counting > 0) {
  110. print("I'm counting from 4 to 1. This is number $counting. ");
  111. $counting--;
  112. }
  113. print("<b>done</b></p>");
  114.  
  115. // Do-while loop
  116. print("<p>");
  117. do {
  118. print("I'm counting from 0 to 4. This is number $counting. ");
  119. $counting++;
  120. } while($counting < 5);
  121. print("<b>done</b></p>");
  122.  
  123.  
  124. // Indexed arrays
  125. $seasons = array('Spring', 'Summer', 'Fall', 'Winter');
  126. print("<p>");
  127. for($x = 0; $x < count($seasons); $x++) {
  128. print("Season $x: $seasons[$x]... ");
  129. }
  130. print("</p>");
  131.  
  132. $seasons[] = 'Extra Winter';
  133. print("<p>");
  134. for($x = 0; $x < count($seasons); $x++) {
  135. print("Season $x: $seasons[$x]... ");
  136. }
  137. print("</p>");
  138.  
  139. // Associative arrays
  140. $moods = array( 'Monday' => 'Sad', 'Wednesday' => 'Pensive', 'Friday' => 'Happy');
  141. print_r($moods);
  142. print("<p>");
  143. foreach($moods as $key => $value) {
  144. print("The key is: $key. The value is: $value. ");
  145. }
  146. print("</p>");
  147.  
  148. // Constants
  149. define('Champions', 'Florida');
  150.  
  151. // Server-side includes:
  152. // include('header.php');
  153. // require('header.php'); // fails if not found
  154. // require_once('header.php');
  155.  
  156. // Redirect the user to a different page by manipulating the header:
  157. // header('Location: logon.php');
  158. // exit();
  159.  
  160. // Sessions
  161. $countie = 0;
  162. if (isset($_SESSION['countie'])) {
  163. $countie = $_SESSION['countie'];
  164. }
  165. $countie++;
  166. print("<p>You have loaded this page $countie time(s).</p>");
  167.  
  168. $_SESSION['countie'] = $countie;
  169. // use session_destroy(); to (you guessed it) delete a session.
  170.  
  171. // Functions
  172. function calc_rect_area($width, $height = 20) {
  173. $tmp_area = $width * $height;
  174. return $tmp_area;
  175. }
  176.  
  177. $rect1 = calc_rect_area(5, 5);
  178. print("<p>Area of a 5x5 rectangle = $rect1.</p>");
  179.  
  180.  
  181. // Access POST and GET variables:
  182. // print("<p>Value of the get parameter named test: {$_GET['test']}.</p>");
  183. // print("<p>Value of the post parameter named test: {$_POST['test']}.</p>");
  184.  
  185. ?>
  186.  
  187. </body>