{"id":3205,"date":"2016-01-25T17:59:04","date_gmt":"2016-01-25T09:59:04","guid":{"rendered":"http:\/\/leybreeze.com\/blog\/?p=3205"},"modified":"2016-02-18T17:09:16","modified_gmt":"2016-02-18T09:09:16","slug":"%e5%86%99%e7%bb%99%e7%a8%8b%e5%ba%8f%e5%91%98%e7%9a%84-php-%e8%af%ad%e6%b3%95%e5%bf%ab%e9%80%9f%e5%85%a5%e9%97%a8","status":"publish","type":"post","link":"http:\/\/leybreeze.com\/blog\/?p=3205","title":{"rendered":"\u5199\u7ed9\u7a0b\u5e8f\u5458\u7684 PHP \u8bed\u6cd5\u5feb\u901f\u5165\u95e8"},"content":{"rendered":"<pre lang=\"php\">\r\n<?php include 'header.php';?>\r\n<?php\r\n \/*\r\n \u672c\u6587\u76ee\u7684:\u603b\u89c8 PHP \u7684\u4e3b\u8981\u8bed\u6cd5\uff0c\u5e76\u4f9b\u65e5\u540e\u5907\u5fd8\u3002\r\n \u9605\u8bfb\u6307\u5357\uff1a\u672c\u6587\u4e0d\u662f\u6587\u6863\u3002\u901a\u8fc7\u901a\u8bfb\u672c\u6587\uff0c\u53ef\u4ee5\u4e86\u89e3 PHP \u57fa\u672c\u7684\u8bed\u6cd5\u53ca\u4e00\u4e9b\u5e38\u7528\u7684\u65b9\u6cd5\u3002\u8bfb\u8005\u53ef\u4ee5\u5c06\u5176\u4e0e\u5176\u4ed6\u8bed\u8a00\u6bd4\u8f83\uff0c\u7406\u89e3\u5176\u610f\u56fe\u4e0e\u542b\u4e49\u3002\r\n \u76ee\u6807\u8bfb\u8005\uff1a\u5177\u6709\u7a0b\u5e8f\u8bbe\u8ba1\u57fa\u7840\u77e5\u8bc6\uff0c\u7406\u89e3\u9762\u5411\u5bf9\u8c61\u7a0b\u5e8f\u8bbe\u8ba1\u57fa\u672c\u6982\u5ff5\u7684\u7a0b\u5e8f\u5458\u3002\r\n *\/\r\n\r\n \/\/ \u5bfc\u5165\r\n include('header.php');\r\n require('core.php');\r\n\r\n \/\/ echo \u5185\u5bb9\u652f\u6301 HTML\r\n echo \"<strong>This is a bold text.<\/strong>\";\r\n\r\n \/\/ \u53d8\u91cf\r\n $NAME = 'BREEZE';\r\n $name = 'breeze'; \/\/ \u53d8\u91cf\u540d\u5bf9\u5927\u5c0f\u5199\u654f\u611f\r\n \/\/ 1ABC = 25; \u53d8\u91cf\u540d\u4e0d\u80fd\u4ee5\u6570\u5b57\u8d77\u59cb\r\n $variable = 'name';\r\n echo $name;\r\n echo $name.$NAME; \/\/ breezeBREEZE\r\n echo $$variable; \/\/ breeze\r\n\r\n \/\/ \u5e38\u91cf\r\n define(\"CASE_SENSITIVE_CONSTANT\",\"CONSTANT_SAMPLE\",true); \/\/ \u5b9a\u4e49\u4e00\u4e2a\u5177\u6709\u5927\u5c0f\u5199\u654f\u611f\u7684\u540d\u79f0\u7684\u5e38\u91cf\r\n define(\"CASE_INSENSITIVE_CONSTANT\",\"CONSTANT_SAMPLE\",false); \/\/ \u5b9a\u4e49\u4e00\u4e2a\u5177\u6709\u5927\u5c0f\u5199\u4e0d\u654f\u611f\u7684\u540d\u79f0\u7684\u5e38\u91cf\r\n define(\"CONSTANT\",\"CONSTANT_SAMPLE\"); \/\/ \u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u5e38\u91cf\u540d\u79f0\u7684\u5927\u5c0f\u5199\u4e0d\u654f\u611f\u3002\u7136\u800c\uff0c\u540d\u79f0\u5927\u5c0f\u5199\u654f\u611f\u7684\u5e38\u91cf\u4f9d\u7136\u4ee5\u5c0f\u5199\u5f62\u5f0f\u50a8\u5b58\uff0c\u56e0\u6b64\u5728\u5b9a\u4e49\u4e00\u4e2a\u5927\u5c0f\u5199\u654f\u611f\u5e38\u91cf\u4e4b\u540e\u518d\u5b9a\u4e49\u4e00\u4e2a\u540c\u540d\u5c0f\u5199\u5e38\u91cf\u5c06\u53d1\u751f\u9519\u8bef\r\n echo CASE_SENSITIVE_CONSTANT;\r\n\r\n $string1 = '9';\r\n $int1 = -42;\r\n $float1 = 42.09;\r\n $boolean1 = true; \/\/ \u53e6\u4e00\u4e2a\u5e03\u5c14\u503c\u4e3a false\r\n echo($string1+int1); \/\/ -33\r\n\r\n \/\/ \u65b9\u6cd5\r\n function getName() { \/\/ \u65b9\u6cd5\u540d\u5bf9\u5927\u5c0f\u5199\u4e0d\u654f\u611f\uff0c\u4e14\u53ea\u80fd\u4ee5\u5b57\u6bcd\u6216\u4e0b\u5212\u7ebf\uff08_\uff09\u8d77\u59cb\r\n  $name = 'Breeze';\r\n  echo $name; \/\/ \u5c06 echo 'Breeze' \u800c\u975e\u4e4b\u524d\u5b9a\u4e49\u7684 'breeze' \r\n  return $name;\r\n }\r\n\r\n function getGlobalName() {\r\n  global $name;\r\n  echo $name; \/\/ \u5c06 echo \u4e4b\u524d\u5b9a\u4e49\u7684 'breeze' \r\n } \/\/ \u8fd4\u56de NULL\r\n\r\n function echoName($name='Alice') {\r\n  echo $name;\r\n }\r\n\r\n echoName('Alice'); \/\/ Alice\r\n echoName(); \/\/ Alice\r\n\r\n \/\/ \u7b97\u672f\r\n $num1 =4;\r\n $num2 =3;\r\n echo $num1\/$num2; \/\/ 1.33333333333\r\n echo $num1%$num2; \/\/ 1\r\n $num1++;\r\n --$num2; \r\n $num2 += $num1; \/\/ $num2 = 7\r\n $t = true;\r\n $f = false;\r\n\r\n \/\/ \u903b\u8f91\u8fd0\u7b97\r\n $booleanResult = $t and $f or $t xor $f && $t || !$f\r\n \r\n if ($num1 > 1 and $num1 < 1 or $num1 >= 0 xor $num2 <= 5 &#038;&#038; $num1 != $num2 || !($num1 <> $num2)) {\r\n } else if ($num1 == $num2) { \/\/ \u76f8\u7b49\r\n  echo $num1++;\r\n } else if ($num1 === $num2) { \/\/ \u76f8\u7b49\u4e14\u7c7b\u578b\u76f8\u540c (\u5168\u7b49)\r\n  echo $num1--;\r\n } else if ($num1 !== $num2) { \/\/ \u4e0d\u7b49\u6216\u7c7b\u578b\u4e0d\u540c (\u4e0d\u5168\u7b49)\r\n  echo $num2++;\r\n } else {\r\n  echo $num2--;\r\n }\r\n\r\n \/\/ \u6570\u7ec4\r\n $peopleNumeric = array(\"Alice\",\"Bob\");\r\n $peopleAssociative = array(\"Alice\"=>\"17\",\"Bob\"=>\"18\");\r\n $peopleMultiDimensional = array(\"Alice\"=>array('C','Z'),\"Bob\"=>array('H','W','L'));\r\n echo $peopleNumber[0]; \/\/ Alice\r\n echo $peopleAssociative['Alice']; \/\/ 17\r\n echo $peopleMultiDimensional['Bob'][2]; \/\/ L\r\n\r\n \/\/ \u5faa\u73af\r\n $intForWhile = 1;\r\n while ($intForWhile<9) {\r\n  echo \"Current: \".$intForWhile;\r\n  $intForWhile++;\r\n }\r\n\r\n do {\r\n  echo \"Current: \".$intForWhile;\r\n  $intForWhile++;\r\n } while($intForWhile<20) \r\n\r\n\r\n for ($i = 0;$i <9;$i++) {\r\n  echo \"Current: \".$i;\r\n  if ($i==7) {\r\n   continue;\r\n  }   \r\n }\r\n\r\n foreach($peopleNumeric as $ $person) {\r\n  echo $person; \/\/ Alice Bob\r\n }\r\n\r\n \/\/ switch\r\n switch ($peopleNumber[0]) {\r\n  case \"Alice\":\r\n   echo \"Alice\";\r\n   break;\r\n  case \"bob\":\r\n  case \"Bob\":\r\n   echo \"Bob\";\r\n   break;\r\n  default:\r\n   echo \"No match\";\r\n }\r\n\r\n\r\n \/\/ superglobals\r\n echo $_SERVER['SCRIPT_NAME']; \/\/ including $GLOBALS, $_REQUEST, $_POST, $_GET, $_FILES, $_ENV, $_COOKIE, $_SESSION\r\n echo $_SERVER['SCRIPT_FILENAME'];\r\n echo $_SERVER['SCRIPT_URL'];\r\n echo $_SERVER['HTTP_HOST'];\r\n echo $_SERVER['PHP_SELF'];\r\n echo $_SERVER['SERVER_ADDR'];\r\n echo $_SERVER['SERVER_NAME'];\r\n echo $_SERVER['SERVER_PORT'];\r\n echo $_SERVER['REMOTE_ADDR'];\r\n echo $_SERVER['REMOTE_HOST'];\r\n echo $_SERVER['REMOTE_PORT'];\r\n\r\n \/\/ \u6587\u4ef6\u64cd\u4f5c\r\n $file=fopen(\"file.txt\", \"w\"); \/\/ mode: r, w, a, x, r+, w+, a+, x+. a mode won't erase data\r\n fwrite($file,\"TEXT\\n\"); \/\/ the original content is erased\r\n fclose($file); \/\/ return true or false\r\n $file=fopen(\"file.txt\", \"a\");\r\n fwrite($file,\"TEXT\\n\"); \/\/ content of the file: TEST\\nTEST\\n\r\n fclose($file);\r\n $content=file(\"file.txt\"); \r\n $count=count($content); \/\/ get length of the array\r\n foreach ($content as $line) {\r\n  echo $line.\"\\n\";\r\n }\r\n\r\n?>\r\n<\/pre>\n<p>post.php \u4ee5 POST \u65b9\u5f0f\u63d0\u4ea4\u8868\u5355<\/p>\n<pre lang=\"html\">\r\n<html>\r\n <body>\r\n  <form action=\"main.php\" method=\"post\"> \/\/ data sent to main.php when the page is submitted. if action is not set, data will be sent to the file itself\r\n   <p>Name: <input type=\"text\" name=\"name\" \/><\/p>\r\n   <p><input type=\"submit\" name=\"submit\" value=\"Submit\" \/><\/p>\r\n  <\/form>\r\n <\/body>\r\n<\/html>\r\n<\/pre>\n<p>main.php \u63a5\u6536 POST<\/p>\n<pre lang=\"html\">\r\n<html>\r\n <body>\r\nWelcome <?\r\nphp echo $_POST[\"name\"]; \/\/ for post method, which is preferred for sending (big) data \r\n?>\r\n <\/body>\r\n<\/html>\r\n<\/pre>\n<p>session_cookie_get.php \u5c06\u6570\u636e\u5b58\u81f3 session \u4e0e Cookie<\/p>\n<pre lang=\"html\">\r\n<?php\r\n session_start();\r\n $_SESSION['temp']='TEST';\r\n setcookie(\"user\", \"Alice\", time()+(86400*7), '\/'); \/\/ must be used before <html> tag\r\n if (isset($_COOKIE['user'])) { \/\/ check if the value has been set\r\n  echo $_COOKIE['user'].\" is set in cookie\";\r\n }\r\n?>\r\n<html>\r\n <body>\r\n  <form action=\"actionGet.php\" method=\"get\">\r\n   <p>data included in URL, e.g., actionGet.php?name=Alice<\/p>\r\n   <p>Name: <input type=\"text\" name=\"name\" \/><\/p>\r\n   <p><input type=\"submit\" name=\"submit\" value=\"Submit\" \/><\/p>\r\n  <\/form>\r\n <\/body>\r\n<\/html>\r\n<\/pre>\n<p>actionGet.php \u63a5\u6536 GET<\/p>\n<pre lang=\"html\">\r\n<html>\r\n <body>\r\nWelcome <?php echo $_GET['name']; ?>\r\n<?php\r\n echo $_SESSION['temp']; \/\/ TEST\r\n session_unset();\r\n session_destroy();\r\n if (isset($_GET['name'])) { \r\n  $file=fopen(\"names.txt\", \"a\"); \r\n  fwrite($file, $_GET['name']); \r\n  fclose($file);\r\n  echo $_COOKIE['user'].\" is set in cookie\";\r\n }\r\n?>\r\n <\/body>\r\n<\/html>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[150],"tags":[149],"_links":{"self":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3205"}],"collection":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3205"}],"version-history":[{"count":22,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3205\/revisions"}],"predecessor-version":[{"id":3236,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3205\/revisions\/3236"}],"wp:attachment":[{"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3205"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/leybreeze.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}