{"id":46925,"date":"2024-12-02T16:51:29","date_gmt":"2024-12-02T08:51:29","guid":{"rendered":"https:\/\/fwq.ai\/blog\/46925\/"},"modified":"2024-12-02T16:51:29","modified_gmt":"2024-12-02T08:51:29","slug":"php%e5%87%bd%e6%95%b0%e7%9a%84%e5%8f%82%e6%95%b0%e5%ae%89%e5%85%a8%e5%a6%82%e4%bd%95%e4%bf%9d%e9%9a%9c%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/46925\/","title":{"rendered":"PHP\u51fd\u6570\u7684\u53c2\u6570\u5b89\u5168\u5982\u4f55\u4fdd\u969c\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP\u51fd\u6570\u7684\u53c2\u6570\u5b89\u5168\u5982\u4f55\u4fdd\u969c\uff1f<\/h1>\n<p>\u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP\u51fd\u6570\u7684\u53c2\u6570\u5b89\u5168\u5982\u4f55\u4fdd\u969c\uff1f\u300b<\/span>\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~<\/p>\n<p>\u4e3a\u4e86\u4fdd\u969c PHP \u51fd\u6570\u53c2\u6570\u5b89\u5168\uff0c\u53ef\u91c7\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u7c7b\u578b\u63d0\u793a\uff1a\u9a8c\u8bc1\u53c2\u6570\u7c7b\u578b\uff0c\u4e0d\u7b26\u5219\u629b\u51fa\u5f02\u5e38\u3002\u6570\u636e\u8fc7\u6ee4\uff1a\u8fc7\u6ee4\u7528\u6237\u8f93\u5165\uff0c\u5220\u9664\u975e\u6cd5\u5b57\u7b26\u6216\u9a8c\u8bc1\u683c\u5f0f\u3002\u767d\u540d\u5355\u9650\u5236\uff1a\u53ea\u5141\u8bb8\u9884\u5b9a\u4e49\u7684\u53c2\u6570\u503c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729854644671b7cb4a6d67.jpg\" class=\"aligncenter\" title=\"PHP\u51fd\u6570\u7684\u53c2\u6570\u5b89\u5168\u5982\u4f55\u4fdd\u969c\uff1f\u63d2\u56fe\" alt=\"PHP\u51fd\u6570\u7684\u53c2\u6570\u5b89\u5168\u5982\u4f55\u4fdd\u969c\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u53c2\u6570\u7684\u5b89\u5168\u4fdd\u969c<\/strong><\/p>\n<p>PHP \u4e2d\u7684\u51fd\u6570\u53c2\u6570\u5b89\u5168\u81f3\u5173\u91cd\u8981\uff0c\u4ee5\u9632\u6b62\u6076\u610f\u7528\u6237\u6ce8\u5165\u6076\u610f\u8f93\u5165\uff0c\u7834\u574f\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u4ee5\u4e0b\u662f\u786e\u4fdd\u51fd\u6570\u53c2\u6570\u5b89\u5168\u7684\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<p><strong>\u7c7b\u578b\u63d0\u793a<\/strong><\/p>\n<p>PHP 7 \u5f15\u5165\u4e86\u7c7b\u578b\u63d0\u793a\uff0c\u53ef\u4ee5\u5e2e\u52a9\u9a8c\u8bc1\u51fd\u6570\u53c2\u6570\u7684\u7c7b\u578b\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>function addNumbers(int $a, int $b): int\n{\n    return $a + $b;\n}<\/pre>\n<p>\u8fd9\u91cc\uff0c<code>addNumbers()<\/code> \u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u6574\u6570\u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\u3002\u5982\u679c\u4f20\u9012\u4e86\u5176\u4ed6\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u5219\u4f1a\u5728\u8fd0\u884c\u65f6\u5f15\u53d1 TypeError \u5f02\u5e38\u3002<\/p>\n<p><strong>\u6570\u636e\u8fc7\u6ee4<\/strong><\/p>\n<p>\u5728\u63a5\u53d7\u7528\u6237\u8f93\u5165\u65f6\uff0c\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8f93\u5165\u8fdb\u884c\u8fc7\u6ee4\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);\nif (!$email) {\n    throw new InvalidArgumentException('Invalid email address');\n}<\/pre>\n<p>\u8fd9\u5c06\u9a8c\u8bc1 POST \u53d8\u91cf\u4e2d\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u662f\u5426\u6709\u6548\u3002<\/p>\n<p><strong>\u767d\u540d\u5355<\/strong><\/p>\n<p>\u9650\u5236\u5141\u8bb8\u7684\u53c2\u6570\u503c\u5230\u4e00\u4e2a\u9884\u5148\u5b9a\u4e49\u7684\u767d\u540d\u5355\u4e2d\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>if (!in_array($action, ['view', 'edit', 'delete'])) {\n    throw new InvalidArgumentException('Invalid action');\n}<\/pre>\n<p>\u8fd9\u5c06\u786e\u4fdd <code>$action<\/code> \u53c2\u6570\u7684\u503c\u53ea\u80fd\u662f &#8216;view&#8217;\u3001&#8217;edit&#8217; \u6216 &#8216;delete&#8217;\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u4fdd\u5b58\u7528\u6237\u8bc4\u8bba\u7684\u51fd\u6570\uff1a<\/p>\n<pre>function saveComment(string $author, string $comment): void\n{\n    \/\/ \u4fdd\u5b58\u8bc4\u8bba\u5230\u6570\u636e\u5e93\n}<\/pre>\n<p>\u4e3a\u4e86\u4fdd\u62a4\u6b64\u51fd\u6570\u514d\u53d7\u6076\u610f\u8f93\u5165\u7684\u5f71\u54cd\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u7c7b\u578b\u63d0\u793a\u548c\u6570\u636e\u8fc7\u6ee4\uff1a<\/p>\n<pre>function saveComment(string $author, string $comment): void\n{\n    \/\/ \u9a8c\u8bc1\u4f5c\u8005\u59d3\u540d\n    $author = filter_var($author, FILTER_SANITIZE_STRING);\n    if (empty($author)) {\n        throw new InvalidArgumentException('Invalid author name');\n    }\n    \n    \/\/ \u9a8c\u8bc1\u8bc4\u8bba\u5185\u5bb9\n    $comment = filter_var($comment, FILTER_SANITIZE_STRING);\n    if (empty($comment)) {\n        throw new InvalidArgumentException('Invalid comment content');\n    }\n    \n    \/\/ \u4fdd\u5b58\u8bc4\u8bba\u5230\u6570\u636e\u5e93\n}<\/pre>\n<p>\u8fd9\u4e9b\u989d\u5916\u7684\u68c0\u67e5\u6709\u52a9\u4e8e\u9632\u6b62\u6076\u610f\u7528\u6237\u6ce8\u5165 SQL \u6ce8\u5165\u653b\u51fb\u6216\u5176\u4ed6\u5b89\u5168\u6f0f\u6d1e\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aPHP\u51fd\u6570\u7684\u53c2\u6570\u5b89\u5168\u5982\u4f55\u4fdd\u969c\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u51fd\u6570\u7684\u53c2\u6570\u5b89\u5168\u5982\u4f55\u4fdd\u969c\uff1f \u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587\u300aPHP\u51fd\u6570\u7684\u53c2\u6570\u5b89\u5168\u5982\u4f55\u4fdd\u969c\uff1f\u300b\u7684\u5185\u5bb9\u4e2d\u5c06\u4f1a\u6d89\u53ca\u5230\u7b49\u7b49\u3002\u5982\u679c\u4f60\u6b63\u5728\u5b66\u4e60\u6587\u7ae0\u76f8\u5173\u77e5\u8bc6\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\uff0c\u4ee5\u540e\u4f1a\u7ed9\u5927\u5bb6\u5e26\u6765\u66f4\u591a\u6587\u7ae0\u76f8\u5173\u6587\u7ae0\uff0c\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u8fdb\u6b65\uff01\u4e0b\u9762\u5c31\u5f00\u59cb\u672c\u6587\u7684\u6b63\u5f0f\u5185\u5bb9~ \u4e3a\u4e86\u4fdd\u969c PHP \u51fd\u6570\u53c2\u6570\u5b89\u5168\uff0c\u53ef\u91c7\u7528\u4ee5\u4e0b\u65b9\u6cd5\uff1a\u7c7b\u578b\u63d0\u793a\uff1a\u9a8c\u8bc1\u53c2\u6570\u7c7b\u578b\uff0c\u4e0d\u7b26\u5219\u629b\u51fa\u5f02\u5e38\u3002\u6570\u636e\u8fc7\u6ee4\uff1a\u8fc7\u6ee4\u7528\u6237\u8f93\u5165\uff0c\u5220\u9664\u975e\u6cd5\u5b57\u7b26\u6216\u9a8c\u8bc1\u683c\u5f0f\u3002\u767d\u540d\u5355\u9650\u5236\uff1a\u53ea\u5141\u8bb8\u9884\u5b9a\u4e49\u7684\u53c2\u6570\u503c\u3002 PHP \u51fd\u6570\u53c2\u6570\u7684\u5b89\u5168\u4fdd\u969c PHP \u4e2d\u7684\u51fd\u6570\u53c2\u6570\u5b89\u5168\u81f3\u5173\u91cd\u8981\uff0c\u4ee5\u9632\u6b62\u6076\u610f\u7528\u6237\u6ce8\u5165\u6076\u610f\u8f93\u5165\uff0c\u7834\u574f\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u4ee5\u4e0b\u662f\u786e\u4fdd\u51fd\u6570\u53c2\u6570\u5b89\u5168\u7684\u51e0\u79cd\u65b9\u6cd5\uff1a \u7c7b\u578b\u63d0\u793a PHP 7 \u5f15\u5165\u4e86\u7c7b\u578b\u63d0\u793a\uff0c\u53ef\u4ee5\u5e2e\u52a9\u9a8c\u8bc1\u51fd\u6570\u53c2\u6570\u7684\u7c7b\u578b\u3002\u4f8b\u5982\uff1a function addNumbers(int $a, int $b): int { return $a + $b; } \u8fd9\u91cc\uff0caddNumbers() \u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u6574\u6570\u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\u3002\u5982\u679c\u4f20\u9012\u4e86\u5176\u4ed6\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u5219\u4f1a\u5728\u8fd0\u884c\u65f6\u5f15\u53d1 TypeError \u5f02\u5e38\u3002 \u6570\u636e\u8fc7\u6ee4 \u5728\u63a5\u53d7\u7528\u6237\u8f93\u5165\u65f6\uff0c\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u6216\u6b63\u5219\u8868\u8fbe\u5f0f\u5bf9\u8f93\u5165\u8fdb\u884c\u8fc7\u6ee4\u3002\u4f8b\u5982\uff1a $email = filter_var($_POST[&#8217;email&#8217;], FILTER_VALIDATE_EMAIL); if (!$email) { throw new InvalidArgumentException(&#8216;Invalid email address&#8217;); } \u8fd9\u5c06\u9a8c\u8bc1 POST \u53d8\u91cf\u4e2d\u7684\u7535\u5b50\u90ae\u4ef6\u5730\u5740\u662f\u5426\u6709\u6548\u3002 \u767d\u540d\u5355 \u9650\u5236\u5141\u8bb8\u7684\u53c2\u6570\u503c\u5230\u4e00\u4e2a\u9884\u5148\u5b9a\u4e49\u7684\u767d\u540d\u5355\u4e2d\u3002\u4f8b\u5982\uff1a if (!in_array($action, [&#8216;view&#8217;, &#8216;edit&#8217;, &#8216;delete&#8217;])) { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-46925","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46925","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=46925"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/46925\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=46925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=46925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=46925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}