{"id":146,"date":"2018-12-10T15:31:43","date_gmt":"2018-12-10T07:31:43","guid":{"rendered":"http:\/\/h-k.pw\/?p=146"},"modified":"2020-06-03T18:36:43","modified_gmt":"2020-06-03T10:36:43","slug":"php","status":"publish","type":"post","link":"https:\/\/harson.co\/index.php\/2018\/12\/10\/php\/","title":{"rendered":"PHP"},"content":{"rendered":"<h2>PHP\u547d\u4ee4\u884c\u542f\u52a8<\/h2>\r\n<pre class=\"pure-highlightjs\"><code class=\"\">php -S 127.0.0.1:8080 -t .\/<\/code><\/pre>\r\n<h2>PHP\u53d8\u91cf<\/h2>\r\n<pre class=\"pure-highlightjs\"><code class=\"\">PHP\u53d8\u91cf\u9700\u8981\u6807\u8bc6\u7b26$\uff0c\u5982\uff1a\r\n$a=10;<\/code><\/pre>\r\n<p><strong>\u53d8\u91cf\u51fd\u6570<\/strong><\/p>\r\n<pre class=\"pure-highlightjs\"><code class=\"\">&lt;?php\r\n    function come($name)\r\n{\r\n    echo \"That\".$name.\" is coming!\";\/\/\u8f93\u51fa\u53d8\u91cf\u5185\u5bb9\uff0c\u5728\u53d8\u91cf\u524d\u540e\u5404\u52a0\u4e00\u4e2a\u70b9\r\n}\r\n\r\n$a = \"come\";\r\n$a(\"Tommy\"); \/\/\u6b64\u65f6a\u53d8\u91cf\u5df2\u7ee7\u627f\u4e86come\u51fd\u6570\u7684\u529f\u80fd\uff0c\u56e0\u6b64\u8fd9\u4e2a\u53d8\u91cf\u80fd\u50cf\u4e00\u4e2a\u51fd\u6570\u4e00\u6837\u8fd0\u884c\r\n?&gt;<\/code><\/pre>\r\n<p><strong>PHP\u9884\u5b9a\u4e49\u53d8\u91cf<\/strong><\/p>\r\n<pre class=\"pure-highlightjs\"><code class=\"\">$_SERVER['SERVER_ADDR'] \/\/\u5f53\u524d\u670d\u52a1\u5730\u5740\r\n$_SERVER['REMOTE_ADDR'] \/\/\u5f53\u524d\u6d4f\u89c8\u7528\u6237\u7684IP\u5730\u5740\r\n<br \/>$_REQUEST \/\/POST\u6216GET\r\n$_POST  \/\/\u7528\u4e8e\u83b7\u53d6POST\u65b9\u6cd5\u63d0\u4ea4\u7684\u6570\u636e\r\n$_GET   \/\/\u7528\u4e8eGET\u65b9\u6cd5\u63d0\u4ea4\u7684\u6570\u636e<\/code><\/pre>\r\n<h2>\u6570\u7ec4<\/h2>\r\n<p>php\u4e2d\u6709\u4e24\u79cd\u6570\u7ec4\uff0c\u5176\u4e2d\u4e00\u79cd\u548cC\u6ca1\u4ec0\u4e48\u533a\u522b\uff0c\u8c03\u7528\u65f6\uff0c\u548cC\u533a\u522b\u4e0d\u5927\u3002\u53e6\u5916\u4e00\u79cd\uff0c\u662f\u952e\u503c\u5bf9\u7c7b\u578b\u7684\u6570\u7ec4\u3002<\/p>\r\n<pre class=\"pure-highlightjs\"><code class=\"\">$name=array(\"Tom\", \"Jimmy\", \"Mike\");\r\n$phone=array(\"1\"=&gt;\"apple\",\"2\"=&gt;\"huawei\", \"3\"=&gt;\"xiaomi\");<\/code><\/pre>\r\n<p><strong>\u4f7f\u7528 foreach \u5faa\u73af\u904d\u5386\u6570\u7ec4<\/strong><\/p>\r\n<pre class=\"pure-highlightjs\"><code class=\"\">foreach($name as $value) \/\/name\u662f\u4e00\u4e2a\u6570\u7ec4, value\u662f\u5f53\u524d\u8fed\u4ee3\u91cf\r\n    ;\r\n\r\nforeach($phone as $key=&gt;$value)\r\n    ;<\/code><\/pre>\r\n<p>foreach \u8bed\u53e5\u4f1a\u4ece\u6570\u7ec4\u7b2c\u4e00\u4e2a\u5143\u7d20\u5f00\u59cb\u904d\u5386\uff0c\u5f53\u524d\u5143\u7d20\u503c\u50a8\u5b58\u5728$value \u53d8\u91cf\u4e2d\uff0c\u7b2c\u4e8c\u79cd\u5219\u53ef\u4ee5\u540c\u65f6\u904d\u5386\u952e\u548c\u503c<\/p>\r\n<p><strong>count() \u51fd\u6570\u7528\u4e8e\u8fd4\u56de\u6570\u7ec4\u7684\u957f\u5ea6<\/strong><\/p>\r\n<p><strong>each() \u7ecf\u5e38\u548c list() \u7ed3\u5408\u4f7f\u7528\u6765\u904d\u5386\u6570\u7ec4\u3002<\/strong><\/p>\r\n<p>each()\u8fd4\u56de\u4e00\u4e2a\u7531\u6570\u7ec4\u5f53\u524d\u5185\u90e8\u6307\u9488\u6240\u6307\u5411\u7684\u5143\u7d20\u7684\u952e\u540d\u548c\u952e\u503c\u7ec4\u6210\u7684\u6570\u7ec4\uff0c\u5e76\u628a\u5185\u90e8\u6307\u9488\u5411\u524d\u79fb\u52a8\u3002<\/p>\r\n<p>\u800clist()\u5219\u7528\u4e8e\u628a\u4e00\u4e2a\u6570\u7ec4\u4e2d\u7684\u952e\u548c\u503c\u5206\u522b\u8d4b\u7ed9\u4e24\u4e2a\u53d8\u91cf\u3002<\/p>\r\n<pre class=\"pure-highlightjs\"><code class=\"\">\r\n$people = array(\"Peter\", \"Joe\", \"Glenn\", \"Cleveland\");\r\n\r\nreset($people);\r\n\r\nwhile (list($key, $val) = each($people))\r\n  {\r\n  echo \"$key =&gt; $val&lt;br \/&gt;\";\r\n  }\r\n?&gt;<\/code><\/pre>\r\n<h4>\u51fd\u6570\u6c47\u603b<\/h4>\r\n<pre class=\"pure-highlightjs\"><code class=\"\">\r\narray_search(\"red\",$arr); \/\/\u5728arr\u6570\u7ec4\u4e2d\u627e\u5230\u4e00\u4e2a\u503c red,\u7136\u540e\u8fd4\u56de\u952e\r\nin_array(\"red\",$arr); \r\narray_push($a,\"blue\",\"yellow\"); \/\/\u5728\u6570\u7ec4\u672b\u5c3e\u52a0\u5165\u5143\u7d20\uff0c\u52a0\u5165\u7684\u5143\u7d20\u952e\u4e3a\u6570\u7ec4\u4e0b\u6807\uff08\u6570\u5b57\uff09\r\narray_unique($a); \/\/\u5220\u9664\u6570\u7ec4\u4e2d\u91cd\u590d\u503c<br \/><\/code><\/pre>\r\n<pre class=\"pure-highlightjs\"><code class=\"\">mb_strpos() \/\/\u8fd4\u56de\u8981\u67e5\u627e\u7684\u5b57\u7b26\u4e32\u5728\u522b\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u9996\u6b21\u51fa\u73b0\u7684\u4f4d\u7f6e\r\n\/\/ mb_strpos (haystack ,needle )\r\n\/\/ haystack\uff1a\u8981\u88ab\u68c0\u67e5\u7684\u5b57\u7b26\u4e32\u3002\r\n\/\/ needle\uff1a\u8981\u641c\u7d22\u7684\u5b57\u7b26\u4e32\r\n\r\nmb_substr() \/\/\u51fd\u6570\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u4e00\u90e8\u5206\u3002\r\n\r\n\/\/str \u5fc5\u9700\u3002\u4ece\u8be5 string \u4e2d\u63d0\u53d6\u5b50\u5b57\u7b26\u4e32\u3002\r\n\/\/start \u5fc5\u9700\u3002\u89c4\u5b9a\u5728\u5b57\u7b26\u4e32\u7684\u4f55\u5904\u5f00\u59cb\u3002\r\n\/\/ength \u53ef\u9009\u3002\u89c4\u5b9a\u8981\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\u957f\u5ea6\u3002\u9ed8\u8ba4\u662f\u76f4\u5230\u5b57\u7b26\u4e32\u7684\u7ed3\u5c3e<br \/><\/code><\/pre>\r\n<pre class=\"pure-highlightjs\"><code class=\"\">\u53ef\u5229\u7528\u8be5\u51fd\u6570\u8f6c\u5b57\u7b26\u4e32\uff08PHP\u4ee3\u7801\uff09\u7ed5\u8fc7\u5b57\u7b26\u68c0\u6d4b\r\nstring base_convert ( string $number , int $frombase , int $tobase )\r\n$number\uff1a\u5f85\u8f6c\u6362\u7684\u6570\u503c\r\n$frombase\uff1a\u6570\u503c\u662f\u51e0\u8fdb\u5236\r\n$tobase\uff1a\u8981\u8f6c\u6362\u4e3a\u51e0\u8fdb\u5236\r\n<\/code><\/pre>\r\n<p>&nbsp;<\/p>\r\n<p><strong>\u5b57\u7b26\u4e32\u64cd\u4f5c<\/strong><\/p>\r\n<pre class=\"pure-highlightjs\"><code class=\"\">\r\nltrim(); \/\/\u53bb\u9664\u7279\u6b8a\u5b57\u7b26\r\naddslashes($str); \/\/\u5bf9\u5b57\u7b26\u4e32\u4e2d\u7279\u6b8a\u5b57\u7b26\u8f6c\u4e49\r\nstripslashes(%st); \/\/\u8f6c\u4e49\u8fd8\u539f\r\nstrlen($len); \/\/\u957f\u5ea6\r\nstrcmp($str,$a); \/\/\u5b57\u7b26\u4e32\u6bd4\u8f83\r\nstr_ireplace(); \/\/\u66ff\u6362\u67e5\u8be2\u5173\u952e\u5b57\r\nstr_replace(); \/\/\u66ff\u6362\u5b57\u7b26\u4e32\r\nnumber_fomat(); \/\/\u5b57\u7b26\u4e32\r\n\r\nexplode(); \/\/\u5206\u5272\u5b57\u7b26\u4e32\r\nexplode(\" \",$str) \/\/\u5c06\u5b57\u7b26\u4e32\u6253\u6563\u6210\u6570\u7ec4\r\n\r\nlist($va1, $va2,...)\u00a0\/\/\u5c06\u4e00\u4e2a\u6570\u7ec4\u8d4b\u7ed9\u5b83\uff0c\u53ef\u7528\u4e8e\u6279\u91cf\u8d4b\u503c<\/code><\/pre>\r\n<p><strong>\u65e5\u671f\u548c\u65f6\u95f4<\/strong><\/p>\r\n<pre class=\"pure-highlightjs\"><code class=\"\">time(); \/\/\u83b7\u53d6\u65f6\u95f4\u6233\r\n\r\ndate_default_timezone_set(\"Asia\/Shanghai\"); \/\/\u8bbe\u7f6e\u65f6\u533a\r\ndate('Y-m-d'); \/\/\u5e74\u6708\u65e5\u65f6\u95f4\r\ndate('Y-m-d h:i:sa'); \/\/\u5e74\u6708\u65e5\u65f6\u5206\u79d212h\r\n\r\ngetdate(); \/\/\u8fd4\u56de\u5305\u542b\u5f53\u524d\u65f6\u95f4\u7684\u6570\u7ec4\uff0c\u4e5f\u53ef\u63a5\u53d7\u4e00\u4e2a\u65f6\u95f4\u6233\u53c2\u6570\r\nbool checkdate(int moth, int day, int year); \/\/\u68c0\u67e5\u65f6\u95f4\u662f\u5426\u6b63\u786e\r\nstrtotime('2017-1-1'); \/\/\u5c06\u5b57\u7b26\u65f6\u95f4\u8f6c\u6362\u4e3a\u65f6\u95f4\u6233\r\n\r\n<\/code><\/pre>\r\n<p><strong>Cookie&amp;Session<\/strong><\/p>\r\n<pre class=\"pure-highlightjs\"><code class=\"\">&lt;!- cookie \u548c session \u90fd\u5fc5\u987b\u5728html\u9875\u9762\u4e4b\u524d-&gt;\r\nsetcookie(\"user\", \"Alex Porter\", time()+3600); \/\/\u521b\u5efaCookie\uff0c\u952e-user\uff0c\u503c-Al.....\r\nif (isset($_COOKIE[\"user\"])) \/\/\u5224\u65ad\u662f\u5426\u6709cookie\r\nsetcookie(\"user\", \"\", time()-3600); \/\/\u5220\u9664cookie\r\n\r\nsession_star();  \/\/\u542f\u52a8session\r\n$_SESSION['user']= $_POST['Username']; \/\/\u53ef\u7528\u6765\u5224\u65ad\u7528\u6237\u79cd\u7c7b\r\n$_SESSION = array(); \/\/ \u6ce8\u9500(\u6e05\u7a7a)\u6240\u6709session\r\nsession_destroy(); \/\/\u5173\u95edsession\r\n\r\nsetcookie(session_name(), session_id(), time()+60, \"\/\"); \/\/\u901a\u8fc7cookie\u8bbe\u7f6esession\u65f6\u95f4\r\n\/\/session id \u662f\u5ba2\u6237\u7aef\u6807\u8bc6\uff0c\u53ea\u8981\u80fd\u4f20\u9012\u5b83\uff0c\u5c31\u80fd\u5b9e\u73b0session\u7684\u529f\u80fd\r\n\r\nsession_cache_limiter('private' \u6216 'pubilc'); \/\/\u8bbe\u7f6esession\u7f13\u5b58\r\n\r\n\/\/session\u6570\u636e\u5e93\u5b58\u50a8\r\n\r\npass\r\n\r\n<\/code><\/pre>\r\n<p><strong>OOP<\/strong><\/p>\r\n<pre class=\"pure-highlightjs\"><code class=\"\">\/\/\u5b9a\u4e49\u7c7b\r\nclass SomeObject\r\n{\r\n....\r\npubilc function __construct($a, $b, $c, ....)\r\n{\r\nthis-&gt;a =$a;\r\n....\r\n}\r\n....\r\n}\r\n\/\/\u5b9e\u4f8b\u5316\u7c7b\r\n$a = new SomeObject(arv1, arv2, ...);<\/code><\/pre>\r\n<p>&nbsp;<\/p>\r\n<p>&nbsp;<\/p>\r\n\r\n<p>&nbsp;<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>PHP\u547d\u4ee4\u884c\u542f\u52a8 php -S 127.0.0.1:8080 -t .\/ PHP\u53d8\u91cf PHP\u53d8\u91cf\u9700\u8981\u6807\u8bc6\u7b26$\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-146","post","type-post","status-publish","format-standard","hentry","category-web"],"_links":{"self":[{"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/posts\/146","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/comments?post=146"}],"version-history":[{"count":21,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/posts\/146\/revisions"}],"predecessor-version":[{"id":502,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/posts\/146\/revisions\/502"}],"wp:attachment":[{"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/media?parent=146"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/categories?post=146"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/harson.co\/index.php\/wp-json\/wp\/v2\/tags?post=146"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}