{"id":47403,"date":"2024-12-02T09:28:39","date_gmt":"2024-12-02T01:28:39","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47403\/"},"modified":"2024-12-02T09:28:39","modified_gmt":"2024-12-02T01:28:39","slug":"php%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e5%bc%82%e5%b8%b8%e5%a4%84%e7%90%86%e8%bf%9b%e8%a1%8c%e5%8d%95%e5%85%83%e6%b5%8b%e8%af%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47403\/","title":{"rendered":"PHP\u4e2d\u5982\u4f55\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP\u4e2d\u5982\u4f55\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5<\/h1>\n<p>\u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662f\u6587\u7ae0\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300aPHP\u4e2d\u5982\u4f55\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p>\u5728 PHP \u5355\u5143\u6d4b\u8bd5\u4e2d\uff0c\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u81f3\u5173\u91cd\u8981\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u6355\u6349\u548c\u5904\u7406\u9884\u671f\u7684\u548c\u610f\u5916\u7684\u9519\u8bef\uff0c\u786e\u4fdd\u6d4b\u8bd5\u7528\u4f8b\u7684\u7a33\u5b9a\u6027\uff1a\u5728 PHP \u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u901a\u8fc7 try&#8230;catch \u5757\u5b8c\u6210\u3002\u5f02\u5e38\u5904\u7406\u6709\u52a9\u4e8e\u6355\u6349\u548c\u9a8c\u8bc1\u9519\u8bef\uff0c\u786e\u4fdd\u6d4b\u8bd5\u7528\u4f8b\u7684\u7a33\u5b9a\u6027\uff0c\u5e76\u63d0\u4f9b\u66f4\u597d\u7684\u9519\u8bef\u62a5\u544a\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729916505671c6e592e50d.jpg\" class=\"aligncenter\" title=\"PHP\u4e2d\u5982\u4f55\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u63d2\u56fe\" alt=\"PHP\u4e2d\u5982\u4f55\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u4e2d\u7684\u5f02\u5e38\u5904\u7406\u4e0e\u5355\u5143\u6d4b\u8bd5<\/strong><\/p>\n<p>\u5f02\u5e38\u5904\u7406\u5728\u5355\u5143\u6d4b\u8bd5\u4e2d\u81f3\u5173\u91cd\u8981\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u6355\u6349\u548c\u5904\u7406\u9884\u671f\u7684\u548c\u610f\u5916\u7684\u9519\u8bef\uff0c\u786e\u4fdd\u6d4b\u8bd5\u7528\u4f8b\u7684\u7a33\u5b9a\u6027\u3002\u5728 PHP \u4e2d\uff0c\u5f02\u5e38\u548c\u9519\u8bef\u5904\u7406\u901a\u8fc7 <code>try...catch<\/code> \u5757\u5b8c\u6210\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u8ba1\u7b97\u4e24\u4e2a\u6570\u5b57\u603b\u548c\u7684\u51fd\u6570 <code>sum()<\/code>:<\/p>\n<pre>function sum($a, $b) {\n  if (!is_numeric($a) || !is_numeric($b)) {\n    throw new IllegalArgumentException('Inputs must be numeric.');\n  }\n  return $a + $b;\n}<\/pre>\n<p><strong>\u5355\u5143\u6d4b\u8bd5\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528\u5f02\u5e38\u5904\u7406\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u5355\u5143\u6d4b\u8bd5\u7528\u4f8b\u6765\u9a8c\u8bc1 <code>sum()<\/code> \u51fd\u6570\u629b\u51fa\u7684\u5f02\u5e38\uff1a<\/p>\n<pre>use PHPUnit\\Framework\\TestCase;\n\nclass SumTest extends TestCase {\n\n  public function testSumNumericInputs() {\n    $this-&gt;assertEquals(5, sum(2, 3));\n  }\n\n  public function testSumNonNumericInputs() {\n    $this-&gt;expectException(IllegalArgumentException::class);\n    sum('a', 'b');\n  }\n}<\/pre>\n<ul>\n<li>\u5728 <code>testSumNonNumericInputs()<\/code> \u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 <code>expectException()<\/code> \u65ad\u8a00\u662f\u5426\u629b\u51fa\u4e86\u6307\u5b9a\u7684\u5f02\u5e38\u3002<\/li>\n<li>\u5bf9\u4e8e\u5176\u4ed6\u573a\u666f\uff0c\u6211\u4eec\u4ecd\u7136\u53ef\u4ee5\u4f7f\u7528\u5e38\u89c4\u65ad\u8a00\u6765\u9a8c\u8bc1\u8fd4\u56de\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<p><strong>\u7ed3\u8bba\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u5bf9\u5355\u5143\u6d4b\u8bd5\u81f3\u5173\u91cd\u8981\uff0c\u5b83\u6709\u52a9\u4e8e\uff1a<\/p>\n<ul>\n<li>\u6355\u6349\u548c\u9a8c\u8bc1\u9519\u8bef<\/li>\n<li>\u786e\u4fdd\u6d4b\u8bd5\u7528\u4f8b\u7684\u7a33\u5b9a\u6027<\/li>\n<li>\u63d0\u4f9b\u66f4\u597d\u7684\u9519\u8bef\u62a5\u544a<\/li>\n<\/ul>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPHP\u4e2d\u5982\u4f55\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u7c73\u4e91\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u4e2d\u5982\u4f55\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5 \u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662f\u6587\u7ae0\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300aPHP\u4e2d\u5982\u4f55\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01 \u5728 PHP \u5355\u5143\u6d4b\u8bd5\u4e2d\uff0c\u4f7f\u7528\u5f02\u5e38\u5904\u7406\u81f3\u5173\u91cd\u8981\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u6355\u6349\u548c\u5904\u7406\u9884\u671f\u7684\u548c\u610f\u5916\u7684\u9519\u8bef\uff0c\u786e\u4fdd\u6d4b\u8bd5\u7528\u4f8b\u7684\u7a33\u5b9a\u6027\uff1a\u5728 PHP \u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u901a\u8fc7 try&#8230;catch \u5757\u5b8c\u6210\u3002\u5f02\u5e38\u5904\u7406\u6709\u52a9\u4e8e\u6355\u6349\u548c\u9a8c\u8bc1\u9519\u8bef\uff0c\u786e\u4fdd\u6d4b\u8bd5\u7528\u4f8b\u7684\u7a33\u5b9a\u6027\uff0c\u5e76\u63d0\u4f9b\u66f4\u597d\u7684\u9519\u8bef\u62a5\u544a\u3002 PHP \u4e2d\u7684\u5f02\u5e38\u5904\u7406\u4e0e\u5355\u5143\u6d4b\u8bd5 \u5f02\u5e38\u5904\u7406\u5728\u5355\u5143\u6d4b\u8bd5\u4e2d\u81f3\u5173\u91cd\u8981\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u4eba\u5458\u6355\u6349\u548c\u5904\u7406\u9884\u671f\u7684\u548c\u610f\u5916\u7684\u9519\u8bef\uff0c\u786e\u4fdd\u6d4b\u8bd5\u7528\u4f8b\u7684\u7a33\u5b9a\u6027\u3002\u5728 PHP \u4e2d\uff0c\u5f02\u5e38\u548c\u9519\u8bef\u5904\u7406\u901a\u8fc7 try&#8230;catch \u5757\u5b8c\u6210\u3002 \u5b9e\u6218\u6848\u4f8b\uff1a \u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u8ba1\u7b97\u4e24\u4e2a\u6570\u5b57\u603b\u548c\u7684\u51fd\u6570 sum(): function sum($a, $b) { if (!is_numeric($a) || !is_numeric($b)) { throw new IllegalArgumentException(&#8216;Inputs must be numeric.&#8217;); } return $a + $b; } \u5355\u5143\u6d4b\u8bd5\uff1a \u4f7f\u7528\u5f02\u5e38\u5904\u7406\uff0c\u6211\u4eec\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u5355\u5143\u6d4b\u8bd5\u7528\u4f8b\u6765\u9a8c\u8bc1 sum() \u51fd\u6570\u629b\u51fa\u7684\u5f02\u5e38\uff1a use PHPUnit\\Framework\\TestCase; class SumTest extends TestCase { public function testSumNumericInputs() { [&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-47403","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47403","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=47403"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47403\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}