PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/General/scratchpad/examples_day_2.php

https://github.com/clarkphp/Code-Examples
PHP | 454 lines | 300 code | 94 blank | 60 comment | 18 complexity | ef34ea8447d6be3704d32ae7d3193eb0 MD5 | raw file
  1. <?php
  2. /* Examples*/
  3. // references
  4. $original = 1;
  5. $ref =& $original;
  6. $ref = 'hello';
  7. echo "ref is $ref and original is $original\n";
  8. //unset($ref);
  9. echo "ref is $ref and original is $original\n";
  10. echo 'ref is ' . (isset($ref) ? '' : ' not ') . ' set' . PHP_EOL;
  11. echo 'original is ' . (isset($original) ? '' : ' not ') . ' set' . PHP_EOL;
  12. $ref = 1;
  13. echo $original;
  14. // passing by reference
  15. $test_var = 'Test value';
  16. function explain_references(&$variable)
  17. {
  18. $variable = 'My new value';
  19. return $variable;
  20. }
  21. echo "before \$test_var is $test_var". $EOLN;
  22. echo explain_references($test_var) . $EOLN;
  23. echo "after \$test_var is $test_var". $EOLN;
  24. exit;
  25. // references to objects
  26. // **if myFunction returns an object**, this notation in PHP4 would make $a a reference to the object.
  27. // This is NOT NECESSARY in PHP 5.
  28. $a[] =& myFunctionReturnsAnObject(); // Do this in PHP 4.
  29. $a[] = myFunctionReturnsAnObject(); // In PHP 5, do this.
  30. // In PHP 4, the second form would give $a a COPY of the object, instead of the actual object itself.
  31. // In PHP 5, it gives you the actual object itself.
  32. function makeArrayUpper($arr) {
  33. if (!is_array($arr)) {
  34. return false;
  35. }
  36. foreach ($arr as $value) {
  37. $array[] = strtoupper($value);
  38. }
  39. return $array;
  40. }
  41. function makeArrayUpper(&$inputArray)
  42. {
  43. if (!is_array($inputArray)) {
  44. return false;
  45. }
  46. foreach ($inputArray as $key => $value) {
  47. $inputArray[$key] = strtoupper($value);
  48. }
  49. return true;
  50. }
  51. //
  52. function makeArrayUpper($input = null)
  53. {
  54. if (!is_array($input)) {
  55. return false;
  56. }
  57. return array_map('strtoupper', $input);
  58. }
  59. $array = explode(' ', 'Zend Training - Building Security into your PHP Applications');
  60. var_dump($array);
  61. makeArrayUpper($array);
  62. var_dump($array);
  63. // list()
  64. var_dump(microtime());
  65. list($usec, $sec) = explode(' ', microtime());
  66. echo $usec . ' ' . ' ' $sec . PHP_EOL;
  67. $info = array('coffee', 'brown', 'caffeine');
  68. $drink = $info[0];
  69. $color = $info[1];
  70. $power = $info[2];
  71. echo "$drink is $color and $power makes it special.$EOLN";
  72. unset($drink, $color, $power);
  73. echo "$drink is $color and $power makes it special.$EOLN";
  74. list($drink, $color, $power) = $info;
  75. echo __LINE__ . " $drink is $color and $power makes it special.$EOLN";
  76. $data = "foo:*:1023:1000::/home/foo:/bin/sh";
  77. list($user, $pass, $uid, $gid, $ucomment, $home, $shell) = explode(":", $data);
  78. echo 'user is ' . $user . $EOLN; // foo
  79. echo 'password is ' . $pass . $EOLN; // *
  80. var_dump($home);
  81. list($myusername,,,,,$home) = explode(":", $data);
  82. //=============================================
  83. $EOL = '<br />';
  84. $dayOfWeek = 'Friday';
  85. if ('Friday' === $dayOfWeek) {
  86. echo "See you next week!$EOL";
  87. } elseif ('Saturday' === $dayOfWeek || 'Sunday' === $dayOfWeek) {
  88. echo "See you on Monday!$EOL";
  89. } else {
  90. echo "See you tomorrow!$EOL";
  91. }
  92. # switch statement
  93. $color = 'Red';
  94. switch ($color) {
  95. case 'Red':
  96. echo 'Red Alert!';
  97. case 'Green':
  98. echo 'All ahead full!';
  99. break;
  100. case 'Blue':
  101. echo 'I can see clearly now; the rain is gone!';
  102. echo 'Hi there!';
  103. break;
  104. case 'Orange':
  105. echo 'As in ... A Clockwork?';
  106. break;
  107. case 'Yellow':
  108. echo 'Banana for Sale, cheap!';
  109. break;
  110. default:
  111. echo 'Not a color I know or like!';
  112. break;
  113. }
  114. echo $color;
  115. $a = true;
  116. $b = false;
  117. switch (true) {
  118. case $a:
  119. case $b:
  120. echo 'Run me now!' . PHP_EOL;
  121. break;
  122. }
  123. exit;
  124. // array iteration
  125. $array = array(
  126. 1 => range(1, 4),
  127. 2 => range(1, 4),
  128. 3 => range(1, 4)
  129. );
  130. foreach ($array as $key => $value) {
  131. if (2 === $key) {
  132. continue;
  133. }
  134. echo $key . ': ' . implode(', ', $value) . "\n";
  135. }
  136. foreach ($array as $value){
  137. foreach ($value as $value2){
  138. echo $value2."<br/>";
  139. }
  140. }
  141. exit;
  142. // curly brace notation for variables
  143. $drink= 5;
  144. echo "${drink}s";
  145. // Uncommon, but correct use of the switch statement.
  146. // Switch is normally used to evaluate a variable against a number of known, literal values.
  147. // I.e., is the color red, green, blue, etc., and then take action accordingly.
  148. // Below, we are switching on the boolean true, and each case has an expression which is being tested.
  149. // I'm making a note of this because this usage appears in the course project, and I wanted you to have a heads-up.
  150. $test=3;
  151. switch(true){
  152. case ($test<6):
  153. echo "yes, less than 6$EOLN";
  154. break;
  155. default:
  156. echo "no, at least 6$EOLN";
  157. }
  158. //# functions you already know
  159. define('CONSTANT', 'Zend to the Resucue!');
  160. echo 'CONSTANT is: ' . CONSTANT . $EOLN;
  161. echo 'Constant is: ' . Constant . $EOLN;
  162. $EOLN = '<br />';
  163. $EOLN = "\n";
  164. # Complete function index at http://www.php.net/manual/en/indexes.php
  165. # Array functions http://www.php.net/manual/en/ref.array.php
  166. # String functions http://www.php.net/manual/en/book.strings.php
  167. function multipleReturnDemo($var)
  168. {
  169. switch ($var) {
  170. case 1:
  171. return 'one';
  172. break; // break statement is never reached
  173. case 2:
  174. return 'two';
  175. break;
  176. default:
  177. return false;
  178. break;
  179. }
  180. // code here is unreachable
  181. }
  182. echo multipleReturnDemo(1) . $EOLN;
  183. echo multipleReturnDemo(2) . $EOLN;
  184. echo multipleReturnDemo(6) . $EOLN;
  185. //echo is a language construct, not a function
  186. echo 'Hello Me' . $EOLN;
  187. echo ('Hello You' . $EOLN);
  188. //echo ('Hello Them', $EOLN); // not legal
  189. echo 'Hello', ' all', ' you', $EOLN; // legal
  190. //$test = echo 'Hi'; // Not legal - cannot use in expressions
  191. $test = print 'Hi'. $EOLN; // This IS legal
  192. var_dump($test);
  193. echo $EOLN;
  194. $test = "some other value";
  195. print_r($test);
  196. echo $EOLN;
  197. $result = print_r($test, true);
  198. echo $result . $EOLN;
  199. $var = 'Hi there!';
  200. echo '$var is ' . (is_array($var) ? '' : 'not ') . "an array.$EOLN";
  201. $var = array('Hi there!');
  202. echo '$var is ' . (is_array($var) ? '' : 'not ') . "an array.$EOLN";
  203. $pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
  204. $pieces = explode(" ", $pizza);
  205. echo $pieces[0] . $EOLN; // piece1
  206. echo $pieces[1] . $EOLN; // piece2
  207. $pieces = explode("piece3", $pizza);
  208. echo $pieces[0] . $EOLN; // piece1 piece2
  209. echo $pieces[1] . $EOLN; // piece4 etc...
  210. if (function_exists('myFunction')) {
  211. $var = myFunction();
  212. }
  213. # strtolower - used in project
  214. $oldString = 'This Is My Original String';
  215. $oldArray = explode(' ', $oldString);
  216. $newString = strtolower($oldString);
  217. var_dump($oldArray);
  218. echo "\$oldString : $oldString$EOLN" . "\$newString: $newString{$EOLN}";
  219. # Return value
  220. function returnValueDemo($argument) {
  221. switch ($argument) {
  222. case 1:
  223. $retVal = $argument . '10';
  224. break;
  225. case 3:
  226. $retVal = $argument . '30';
  227. break;
  228. case 7:
  229. return false;
  230. break;
  231. case 5:
  232. $retVal = $argument . '50';
  233. break;
  234. default:
  235. $retVal = '';
  236. break;
  237. }
  238. return $retVal;
  239. }
  240. var_dump(returnValueDemo(1)); echo '<br />';
  241. var_dump(returnValueDemo(3)); echo '<br />';
  242. var_dump(returnValueDemo(7)); echo '<br />';
  243. var_dump(returnValueDemo('blecko')); echo '<br />';
  244. # References
  245. $color = 'red';
  246. $Farbe = &$color; // Farbe is German for English 'color'
  247. echo "\$Farbe is $Farbe <br />";
  248. echo "\$color is $color <br />";
  249. echo "\$color is $Farbe <br />";
  250. echo "\$Farbe is $color <br />";
  251. var_dump($Farbe === $color); echo '<br />';
  252. $my_array = array('beef', 'chicken', 'potato');
  253. $reference = &$my_array[1];
  254. $reference = 'sausage';
  255. echo $reference . '<br />';
  256. var_dump($my_array);
  257. // http://www.php.net/manual/en/language.references.php
  258. # Internal functions demo
  259. $original = 'This Is A String Of Mixed Case INCLUDING ALL CAPS';
  260. $caseFoldedString = strtolower($original);
  261. echo "\$original $original<br />";
  262. echo "\$caseFoldedString $caseFoldedString<br />";
  263. $originalArray = explode(' ', $original);
  264. echo '<pre>'; var_dump ($originalArray); echo '</pre>';
  265. sort($originalArray);
  266. echo '<pre>'; var_dump ($originalArray); echo '</pre>';
  267. exit;
  268. // http://www.php.net/manual/en/book.array.php
  269. # File-related predefined constants
  270. echo DIRECTORY_SEPARATOR . "\n" . PATH_SEPARATOR . "\n";
  271. # Show me all defined constants.
  272. define("MY_CONSTANT", 1);
  273. print_r(get_defined_constants(true)); // optional TRUE categorizes them
  274. # What is this context stuff?
  275. #http://php.net/manual/en/intro.stream.php
  276. #http://www.php.net/manual/en/context.php
  277. $zend_home = file_get_contents('http://www.zend.com/');
  278. echo strlen($zend_home) . '<br />';
  279. exit;
  280. //
  281. echo get_include_path() . '<br />';
  282. $fp = fopen('testfile.txt', 'r') or exit('Could not open file!<br />');
  283. if (! $fp) {
  284. echo 'There was an error opening the file<br />';
  285. }
  286. $contents = fgetc($fp);
  287. echo $contents . '<br />';
  288. $contents = fgets($fp);
  289. echo $contents . '<br />';
  290. $contents = fgets($fp, 1000000);
  291. echo $contents . '<br />';
  292. exit;
  293. $EOLN = "\r\n"; // Windows text line ending char sequence
  294. //fgets(); // get a from the file
  295. # Reading from files
  296. fgetc();
  297. fgetcsv();
  298. fgetss();
  299. fscanf();
  300. rewind();
  301. file_get_contents();
  302. # Writing to files
  303. $fp_write = fopen('mytestfile.txt', 'w');
  304. fwrite($fp_write, "Some data to write.$EOLN");
  305. fputs($fp_write, "Some more data to write.$EOLN");
  306. // fputs is an alias for fwrite - same function.
  307. fclose($fp_write);
  308. $fp = fopen('mytestfile.txt', 'a+'); // append to file
  309. $csv_data = array('Column1', 'Column2', 'Column3', 'Column4', 'Column5');
  310. fputcsv($fp, $csv_data); // try using "\t" as third parameter
  311. fclose($fp);
  312. echo 'At line ' . __LINE__;
  313. $fp = fopen('mytestfile.txt', 'r') or exit('Error!');
  314. # Locking files
  315. // open the file first
  316. $fp = fopen('lockfile.txt', 'r+');
  317. //flock
  318. # Closing files and Other file functions
  319. $fp = fopen('myfile.txt', 'r');
  320. $result = fclose($fp);
  321. echo ($result ? 'Successfully closed.' : 'Close failed!') . '<br />';
  322. $lines = file('myfile.txt');
  323. echo '$lines: <pre>' . print_r($lines, true) . '</pre><br />';
  324. $zend_home = file('http://www.zend.com/');
  325. $textFiles = glob('*.txt');
  326. $fileOrPathName = 'ce_samples3.txt';
  327. //$fileOrPathName = 'blecko.jnk';
  328. if (is_dir($fileOrPathName)) {
  329. echo "$fileOrPathName is a directory<br />";
  330. } else {
  331. echo "$fileOrPathName is not a directory<br />";
  332. }
  333. if (is_file($fileOrPathName)) {
  334. echo "$fileOrPathName is a file<br />";
  335. } else {
  336. echo "$fileOrPathName is not a file<br />";
  337. }
  338. tmpfile();
  339. //fpassthru();
  340. # Using fopen() and fclose()
  341. $fh = fopen('myfile.txt', 'r') or die("Cannot open file<br />");
  342. // do something here
  343. fclose($fh);
  344. //
  345. //
  346. $fh = fopen('myfile.txt', 'r') or die("Cannot open file<br />");
  347. while (! feof($fh)) {
  348. echo fgets($fh) . '<br />';
  349. }
  350. fclose($fh);
  351. echo $sec . ' ' . $usec . "\n";
  352. $dirs = glob('../application/views/scripts/*');
  353. foreach ($dirs as $dir) {
  354. foreach( glob("$dir/*") as $file) {
  355. echo "$file - ";
  356. echo filesize($file).'bytes - ';
  357. echo count(file($file));
  358. echo '<br />';
  359. }
  360. }