{"id":47753,"date":"2024-12-02T17:05:52","date_gmt":"2024-12-02T09:05:52","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47753\/"},"modified":"2024-12-02T17:05:52","modified_gmt":"2024-12-02T09:05:52","slug":"php%e5%87%bd%e6%95%b0%e6%b5%8b%e8%af%95%e4%b8%8e%e8%b0%83%e8%af%95%e6%9c%89%e4%bb%80%e4%b9%88%e9%9c%80%e8%a6%81%e6%b3%a8%e6%84%8f%e7%9a%84%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47753\/","title":{"rendered":"PHP\u51fd\u6570\u6d4b\u8bd5\u4e0e\u8c03\u8bd5\u6709\u4ec0\u4e48\u9700\u8981\u6ce8\u610f\u7684\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP\u51fd\u6570\u6d4b\u8bd5\u4e0e\u8c03\u8bd5\u6709\u4ec0\u4e48\u9700\u8981\u6ce8\u610f\u7684\uff1f<\/h1>\n<p><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP\u51fd\u6570\u6d4b\u8bd5\u4e0e\u8c03\u8bd5\u6709\u4ec0\u4e48\u9700\u8981\u6ce8\u610f\u7684\uff1f\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01<\/p>\n<p>PHP \u51fd\u6570\u6d4b\u8bd5\u548c\u8c03\u8bd5\u7684\u5173\u952e\u6ce8\u610f\u4e8b\u9879\u5305\u62ec\uff1a\u5355\u5143\u6d4b\u8bd5\uff1a\u4f7f\u7528\u6846\u67b6\u81ea\u52a8\u5316\u6d4b\u8bd5\u8fc7\u7a0b\uff0c\u7f16\u5199\u6e05\u6670\u7684\u65ad\u8a00\u3002\u8f93\u5165\u9a8c\u8bc1\uff1a\u68c0\u67e5\u6570\u636e\u7c7b\u578b\u3001\u8303\u56f4\u548c\u8fc7\u6ee4\u8f93\u5165\u3002\u5f02\u5e38\u5904\u7406\uff1a\u4f7f\u7528 try\/catch \u5757\u5904\u7406\u51fd\u6570\u6545\u969c\u3002\u65e5\u5fd7\u8bb0\u5f55\u548c\u8ddf\u8e2a\uff1a\u4f7f\u7528\u65e5\u5fd7\u51fd\u6570\u548c\u8c03\u8bd5\u5668\u67e5\u770b\u4e8b\u4ef6\u548c\u51fd\u6570\u8c03\u7528\u987a\u5e8f\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729934507671cb4ab95343.jpg\" class=\"aligncenter\" title=\"PHP\u51fd\u6570\u6d4b\u8bd5\u4e0e\u8c03\u8bd5\u6709\u4ec0\u4e48\u9700\u8981\u6ce8\u610f\u7684\uff1f\u63d2\u56fe\" alt=\"PHP\u51fd\u6570\u6d4b\u8bd5\u4e0e\u8c03\u8bd5\u6709\u4ec0\u4e48\u9700\u8981\u6ce8\u610f\u7684\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u6d4b\u8bd5\u4e0e\u8c03\u8bd5\u7684\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<p>\u5728 PHP \u4e2d\uff0c\u5bf9\u51fd\u6570\u8fdb\u884c\u6d4b\u8bd5\u548c\u8c03\u8bd5\u5bf9\u4e8e\u786e\u4fdd\u4ee3\u7801\u7684\u6b63\u786e\u6027\u548c\u5065\u58ee\u6027\u81f3\u5173\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u9700\u8981\u8003\u8651\u7684\u5173\u952e\u65b9\u9762\uff1a<\/p>\n<p><strong>\u5355\u5143\u6d4b\u8bd5<\/strong><\/p>\n<ul>\n<li>\u5355\u5143\u6d4b\u8bd5\u662f\u9694\u79bb\u6d4b\u8bd5\u51fd\u6570\u7684\u6709\u6548\u65b9\u6cd5\u3002<\/li>\n<li>\u4f7f\u7528\u6846\u67b6\uff08\u4f8b\u5982 PHPUnit\uff09\u53ef\u4ee5\u81ea\u52a8\u5316\u6d4b\u8bd5\u8fc7\u7a0b\u3002<\/li>\n<li>\u7f16\u5199\u6e05\u6670\u3001\u7b80\u6d01\u7684\u6d4b\u8bd5\u65ad\u8a00\uff0c\u907f\u514d\u6a21\u7cca\u6216\u4e0d\u76f8\u5173\u7684\u9519\u8bef\u6d88\u606f\u3002<\/li>\n<\/ul>\n<p><strong>\u8f93\u5165\u9a8c\u8bc1<\/strong><\/p>\n<ul>\n<li>\u5728\u51fd\u6570\u4e2d\u9a8c\u8bc1\u8f93\u5165\u81f3\u5173\u91cd\u8981\uff0c\u4ee5\u9632\u6b62\u6f5c\u5728\u7684\u9519\u8bef\u6216\u6ce8\u5165\u653b\u51fb\u3002<\/li>\n<li>\u4f7f\u7528\u6570\u636e\u7c7b\u578b\u68c0\u67e5\u3001\u8303\u56f4\u9a8c\u8bc1\u548c\u8fc7\u6ee4\u51fd\u6570\u786e\u4fdd\u8f93\u5165\u7684\u6709\u6548\u6027\u3002<\/li>\n<\/ul>\n<p><strong>\u5f02\u5e38\u5904\u7406<\/strong><\/p>\n<ul>\n<li>\u5f02\u5e38\u63d0\u4f9b\u4e86\u5904\u7406\u51fd\u6570\u6545\u969c\u7684\u4fbf\u6377\u65b9\u6cd5\u3002<\/li>\n<li>\u5728\u53ef\u80fd\u51fa\u73b0\u5f02\u5e38\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528 try\/catch \u5757\u3002<\/li>\n<li>\u7f16\u5199\u53ef\u8bfb\u7684\u5f02\u5e38\u6d88\u606f\uff0c\u4ee5\u4fbf\u4e8e\u5feb\u901f\u8c03\u8bd5\u3002<\/li>\n<\/ul>\n<p><strong>\u65e5\u5fd7\u8bb0\u5f55\u548c\u8ddf\u8e2a<\/strong><\/p>\n<ul>\n<li>\u65e5\u5fd7\u8bb0\u5f55\u548c\u8ddf\u8e2a\u53ef\u4ee5\u63d0\u4f9b\u6709\u5173\u51fd\u6570\u6267\u884c\u671f\u95f4\u53d1\u751f\u7684\u4e8b\u4ef6\u7684\u5b9d\u8d35\u4fe1\u606f\u3002<\/li>\n<li>\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u51fd\u6570\uff08\u4f8b\u5982 PHP&#8217;s <code>error_log<\/code>) \u8bb0\u5f55\u5173\u952e\u4e8b\u4ef6\u548c\u9519\u8bef\u3002<\/li>\n<li>\u4f7f\u7528\u8c03\u8bd5\u5668\uff08\u4f8b\u5982 Xdebug\uff09\u8fdb\u884c\u9010\u6b65\u8c03\u8bd5\uff0c\u67e5\u770b\u53d8\u91cf\u7684\u503c\u548c\u51fd\u6570\u8c03\u7528\u987a\u5e8f\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b <code>calculateDiscount<\/code> \u51fd\u6570\uff1a<\/p>\n<pre>function calculateDiscount($amount, $rate)\n{\n    if ($amount &lt; 0 || $rate &lt; 0 || $rate &gt; 100) {\n        throw new InvalidArgumentException(\"Invalid arguments provided.\");\n    }\n\n    return $amount * ($rate \/ 100);\n}<\/pre>\n<p><strong>\u5355\u5143\u6d4b\u8bd5<\/strong><\/p>\n<p>\u53ef\u4ee5\u4f7f\u7528 PHPUnit \u521b\u5efa\u4ee5\u4e0b\u6d4b\u8bd5\uff1a<\/p>\n<pre>use PHPUnit\\Framework\\TestCase;\n\nclass CalculateDiscountTest extends TestCase\n{\n    public function testValidDiscount()\n    {\n        $this-&gt;assertEquals(50, calculateDiscount(100, 50));\n    }\n\n    public function testInvalidAmount()\n    {\n        $this-&gt;expectException(InvalidArgumentException::class);\n        calculateDiscount(-100, 50); \/\/ \u5e94\u8be5\u629b\u51fa\u5f02\u5e38\n    }\n}<\/pre>\n<p><strong>\u5f02\u5e38\u5904\u7406<\/strong><\/p>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>InvalidArgumentException<\/code> \u88ab\u7528\u4e8e\u5904\u7406\u65e0\u6548\u53c2\u6570\u3002\u8fd9\u5141\u8bb8\u6211\u4eec\u5728\u51fd\u6570\u5185\u90e8\u68c0\u6d4b\u9519\u8bef\u5e76\u8fd4\u56de\u6709\u610f\u4e49\u7684\u6d88\u606f\u3002<\/p>\n<p><strong>\u65e5\u5fd7\u8bb0\u5f55<\/strong><\/p>\n<p>\u5982\u679c\u51fd\u6570\u9700\u8981\u6267\u884c\u957f\u65f6\u95f4\u7684\u64cd\u4f5c\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u6765\u8bb0\u5f55\u5176\u8fdb\u5ea6\u6216\u4efb\u4f55\u9047\u5230\u7684\u95ee\u9898\u3002\u4f8b\u5982\uff1a<\/p>\n<pre>error_log(\"Discount calculation started for amount: $amount\");<\/pre>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300aPHP\u51fd\u6570\u6d4b\u8bd5\u4e0e\u8c03\u8bd5\u6709\u4ec0\u4e48\u9700\u8981\u6ce8\u610f\u7684\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8ephp,\u8c03\u8bd5\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u51fd\u6570\u6d4b\u8bd5\u4e0e\u8c03\u8bd5\u6709\u4ec0\u4e48\u9700\u8981\u6ce8\u610f\u7684\uff1f \u6587\u7ae0\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u300aPHP\u51fd\u6570\u6d4b\u8bd5\u4e0e\u8c03\u8bd5\u6709\u4ec0\u4e48\u9700\u8981\u6ce8\u610f\u7684\uff1f\u300b\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01 PHP \u51fd\u6570\u6d4b\u8bd5\u548c\u8c03\u8bd5\u7684\u5173\u952e\u6ce8\u610f\u4e8b\u9879\u5305\u62ec\uff1a\u5355\u5143\u6d4b\u8bd5\uff1a\u4f7f\u7528\u6846\u67b6\u81ea\u52a8\u5316\u6d4b\u8bd5\u8fc7\u7a0b\uff0c\u7f16\u5199\u6e05\u6670\u7684\u65ad\u8a00\u3002\u8f93\u5165\u9a8c\u8bc1\uff1a\u68c0\u67e5\u6570\u636e\u7c7b\u578b\u3001\u8303\u56f4\u548c\u8fc7\u6ee4\u8f93\u5165\u3002\u5f02\u5e38\u5904\u7406\uff1a\u4f7f\u7528 try\/catch \u5757\u5904\u7406\u51fd\u6570\u6545\u969c\u3002\u65e5\u5fd7\u8bb0\u5f55\u548c\u8ddf\u8e2a\uff1a\u4f7f\u7528\u65e5\u5fd7\u51fd\u6570\u548c\u8c03\u8bd5\u5668\u67e5\u770b\u4e8b\u4ef6\u548c\u51fd\u6570\u8c03\u7528\u987a\u5e8f\u3002 PHP \u51fd\u6570\u6d4b\u8bd5\u4e0e\u8c03\u8bd5\u7684\u6ce8\u610f\u4e8b\u9879 \u5728 PHP \u4e2d\uff0c\u5bf9\u51fd\u6570\u8fdb\u884c\u6d4b\u8bd5\u548c\u8c03\u8bd5\u5bf9\u4e8e\u786e\u4fdd\u4ee3\u7801\u7684\u6b63\u786e\u6027\u548c\u5065\u58ee\u6027\u81f3\u5173\u91cd\u8981\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u9700\u8981\u8003\u8651\u7684\u5173\u952e\u65b9\u9762\uff1a \u5355\u5143\u6d4b\u8bd5 \u5355\u5143\u6d4b\u8bd5\u662f\u9694\u79bb\u6d4b\u8bd5\u51fd\u6570\u7684\u6709\u6548\u65b9\u6cd5\u3002 \u4f7f\u7528\u6846\u67b6\uff08\u4f8b\u5982 PHPUnit\uff09\u53ef\u4ee5\u81ea\u52a8\u5316\u6d4b\u8bd5\u8fc7\u7a0b\u3002 \u7f16\u5199\u6e05\u6670\u3001\u7b80\u6d01\u7684\u6d4b\u8bd5\u65ad\u8a00\uff0c\u907f\u514d\u6a21\u7cca\u6216\u4e0d\u76f8\u5173\u7684\u9519\u8bef\u6d88\u606f\u3002 \u8f93\u5165\u9a8c\u8bc1 \u5728\u51fd\u6570\u4e2d\u9a8c\u8bc1\u8f93\u5165\u81f3\u5173\u91cd\u8981\uff0c\u4ee5\u9632\u6b62\u6f5c\u5728\u7684\u9519\u8bef\u6216\u6ce8\u5165\u653b\u51fb\u3002 \u4f7f\u7528\u6570\u636e\u7c7b\u578b\u68c0\u67e5\u3001\u8303\u56f4\u9a8c\u8bc1\u548c\u8fc7\u6ee4\u51fd\u6570\u786e\u4fdd\u8f93\u5165\u7684\u6709\u6548\u6027\u3002 \u5f02\u5e38\u5904\u7406 \u5f02\u5e38\u63d0\u4f9b\u4e86\u5904\u7406\u51fd\u6570\u6545\u969c\u7684\u4fbf\u6377\u65b9\u6cd5\u3002 \u5728\u53ef\u80fd\u51fa\u73b0\u5f02\u5e38\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528 try\/catch \u5757\u3002 \u7f16\u5199\u53ef\u8bfb\u7684\u5f02\u5e38\u6d88\u606f\uff0c\u4ee5\u4fbf\u4e8e\u5feb\u901f\u8c03\u8bd5\u3002 \u65e5\u5fd7\u8bb0\u5f55\u548c\u8ddf\u8e2a \u65e5\u5fd7\u8bb0\u5f55\u548c\u8ddf\u8e2a\u53ef\u4ee5\u63d0\u4f9b\u6709\u5173\u51fd\u6570\u6267\u884c\u671f\u95f4\u53d1\u751f\u7684\u4e8b\u4ef6\u7684\u5b9d\u8d35\u4fe1\u606f\u3002 \u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55\u51fd\u6570\uff08\u4f8b\u5982 PHP&#8217;s error_log) \u8bb0\u5f55\u5173\u952e\u4e8b\u4ef6\u548c\u9519\u8bef\u3002 \u4f7f\u7528\u8c03\u8bd5\u5668\uff08\u4f8b\u5982 Xdebug\uff09\u8fdb\u884c\u9010\u6b65\u8c03\u8bd5\uff0c\u67e5\u770b\u53d8\u91cf\u7684\u503c\u548c\u51fd\u6570\u8c03\u7528\u987a\u5e8f\u3002 \u5b9e\u6218\u6848\u4f8b \u8003\u8651\u4ee5\u4e0b calculateDiscount \u51fd\u6570\uff1a function calculateDiscount($amount, $rate) { if ($amount &lt; 0 || $rate &lt; 0 || $rate &gt; 100) { throw [&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-47753","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47753","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=47753"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47753\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}