{"id":48266,"date":"2024-12-02T17:44:02","date_gmt":"2024-12-02T09:44:02","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48266\/"},"modified":"2024-12-02T17:44:02","modified_gmt":"2024-12-02T09:44:02","slug":"php%e5%87%bd%e6%95%b0%e9%94%99%e8%af%af%e7%9a%84%e6%8e%92%e6%9f%a5%e3%80%81%e5%88%86%e6%9e%90%e5%92%8c%e8%a7%a3%e5%86%b3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48266\/","title":{"rendered":"php\u51fd\u6570\u9519\u8bef\u7684\u6392\u67e5\u3001\u5206\u6790\u548c\u89e3\u51b3"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>php\u51fd\u6570\u9519\u8bef\u7684\u6392\u67e5\u3001\u5206\u6790\u548c\u89e3\u51b3<\/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\u9519\u8bef\u7684\u6392\u67e5\u3001\u5206\u6790\u548c\u89e3\u51b3\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>\u8981\u6392\u67e5\u3001\u5206\u6790\u548c\u89e3\u51b3 PHP \u51fd\u6570\u9519\u8bef\uff0c\u8bf7\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u4f7f\u7528 try-catch \u8bed\u53e5\u6355\u83b7\u51fd\u6570\u629b\u51fa\u7684\u5f02\u5e38\u3002\u8bfb\u53d6\u5f02\u5e38\u6d88\u606f\uff0c\u4e86\u89e3\u95ee\u9898\u7684\u6839\u6e90\u3002\u6839\u636e\u5206\u6790\u7ed3\u679c\u4fee\u590d\u4ee3\u7801\uff0c\u786e\u4fdd\u53c2\u6570\u6709\u6548\u4e14\u7b26\u5408\u51fd\u6570\u8981\u6c42\u3002\u8003\u8651\u5728\u51fd\u6570\u4e2d\u6dfb\u52a0\u989d\u5916\u7684\u9a8c\u8bc1\u4ee5\u9632\u6b62\u9519\u8bef\u518d\u6b21\u53d1\u751f\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241027\/1730006080671dcc403cf78.jpg\" class=\"aligncenter\" title=\"php\u51fd\u6570\u9519\u8bef\u7684\u6392\u67e5\u3001\u5206\u6790\u548c\u89e3\u51b3\u63d2\u56fe\" alt=\"php\u51fd\u6570\u9519\u8bef\u7684\u6392\u67e5\u3001\u5206\u6790\u548c\u89e3\u51b3\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u9519\u8bef\u6392\u67e5\u3001\u5206\u6790\u548c\u89e3\u51b3<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>PHP \u51fd\u6570\u9519\u8bef\u662f\u8f6f\u4ef6\u5f00\u53d1\u4e2d\u5e38\u89c1\u7684\u70e6\u607c\u3002\u672c\u6587\u5c06\u901a\u8fc7\u5b9e\u6218\u6848\u4f8b\uff0c\u5f15\u5bfc\u60a8\u5faa\u5e8f\u6e10\u8fdb\u5730\u6392\u67e5\u3001\u5206\u6790\u548c\u89e3\u51b3\u51fd\u6570\u9519\u8bef\uff0c\u4ece\u800c\u63d0\u9ad8\u60a8\u7684 PHP \u7f16\u7a0b\u6280\u80fd\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a <code>calculateDiscount()<\/code> \u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u6839\u636e\u7ed9\u5b9a\u7684\u6761\u4ef6\u8ba1\u7b97\u6298\u6263\u3002<\/p>\n<pre>function calculateDiscount($subtotal, $discountPercentage) {\n  if (!is_numeric($subtotal)) {\n    throw new InvalidArgumentException(\"Subtotal must be a number.\");\n  } elseif (!is_numeric($discountPercentage) || $discountPercentage &lt; 0 || $discountPercentage &gt; 1) {\n    throw new InvalidArgumentException(\"Discount percentage must be a number between 0 and 1.\");\n  }\n\n  $discount = $subtotal * $discountPercentage;\n  return $subtotal - $discount;\n}<\/pre>\n<p><strong>\u6392\u67e5\u9519\u8bef<\/strong><\/p>\n<p>\u8981\u6392\u67e5\u51fd\u6570\u9519\u8bef\uff0c\u53ef\u4ee5\u4f7f\u7528 PHP <code>try-catch<\/code> \u8bed\u53e5\uff1a<\/p>\n<pre>try {\n  $discountedPrice = calculateDiscount(100, 0.1);\n  echo \"Discounted price: $discountedPrice\";\n} catch (InvalidArgumentException $e) {\n  echo \"Error: \" . $e-&gt;getMessage();\n}<\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>try<\/code> \u5757\u5c06\u6267\u884c <code>calculateDiscount()<\/code> \u51fd\u6570\uff0c\u800c <code>catch<\/code> \u5757\u5c06\u6355\u83b7\u5e76\u5904\u7406\u51fd\u6570\u629b\u51fa\u7684\u4efb\u4f55\u5f02\u5e38\u3002<\/p>\n<p><strong>\u5206\u6790\u9519\u8bef<\/strong><\/p>\n<p>\u5982\u679c\u51fd\u6570\u629b\u51fa\u5f02\u5e38\uff0c<code>catch<\/code> \u5757\u4e2d\u7684 <code>$e-&gt;getMessage()<\/code> \u5c06\u63d0\u4f9b\u9519\u8bef\u6d88\u606f\u3002\u4ed4\u7ec6\u9605\u8bfb\u9519\u8bef\u6d88\u606f\uff0c\u4ee5\u4e86\u89e3\u95ee\u9898\u7684\u6839\u6e90\u3002<\/p>\n<p>\u5728\u8fd9\u4e2a\u6848\u4f8b\u4e2d\uff0c\u9519\u8bef\u53ef\u80fd\u662f\u7531\u4e8e\u4f20\u9012\u4e86\u65e0\u6548\u7684\u53c2\u6570\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li><code>subtotal<\/code> \u4e0d\u662f\u4e00\u4e2a\u6570\u5b57<\/li>\n<li><code>discountPercentage<\/code> \u4e0d\u662f\u4e00\u4e2a\u4ecb\u4e8e 0 \u548c 1 \u4e4b\u95f4\u7684\u6570\u5b57<\/li>\n<\/ul>\n<p><strong>\u89e3\u51b3\u9519\u8bef<\/strong><\/p>\n<p>\u6839\u636e\u5206\u6790\u7ed3\u679c\uff0c\u53ef\u4ee5\u4fee\u590d\u4ee3\u7801\u4ee5\u89e3\u51b3\u9519\u8bef\uff1a<\/p>\n<ol>\n<li>\u786e\u4fdd <code>subtotal<\/code> \u548c <code>discountPercentage<\/code> \u662f\u6709\u6548\u7684\u6570\u5b57<\/li>\n<li>\u9a8c\u8bc1 <code>discountPercentage<\/code> \u7684\u503c\u4ecb\u4e8e 0 \u548c 1 \u4e4b\u95f4<\/li>\n<\/ol>\n<p><strong>\u9884\u9632\u9519\u8bef<\/strong><\/p>\n<p>\u4e3a\u4e86\u9632\u6b62\u9519\u8bef\u518d\u6b21\u53d1\u751f\uff0c\u53ef\u4ee5\u5728\u51fd\u6570\u4e2d\u6dfb\u52a0\u989d\u5916\u7684\u9a8c\u8bc1\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>function calculateDiscount($subtotal, $discountPercentage) {\n  if (!is_numeric($subtotal)) {\n    return; \/\/ \u8fd4\u56de\u7a7a\u503c\uff0c\u8868\u793a\u65e0\u6548\u7684\u8f93\u5165\n  } elseif (!is_numeric($discountPercentage) || $discountPercentage &lt; 0 || $discountPercentage &gt; 1) {\n    return; \/\/ \u8fd4\u56de\u7a7a\u503c\uff0c\u8868\u793a\u65e0\u6548\u7684\u8f93\u5165\n  }\n\n  \/\/ ...\u539f\u6709\u7684\u4ee3\u7801\n}<\/pre>\n<p>\u901a\u8fc7\u6dfb\u52a0\u8fd9\u4e9b\u9a8c\u8bc1\uff0c\u51fd\u6570\u5c06\u5728\u4f20\u9012\u65e0\u6548\u53c2\u6570\u65f6\u8fd4\u56de <code>null<\/code>\uff0c\u800c\u4e0d\u662f\u629b\u51fa\u5f02\u5e38\uff0c\u4ece\u800c\u5728\u4ee3\u7801\u4e2d\u63d0\u4f9b\u66f4\u4f18\u96c5\u7684\u9519\u8bef\u5904\u7406\u3002<\/p>\n<p>\u6587\u4e2d\u5173\u4e8ephp,\u62a5\u9519\u7684\u77e5\u8bc6\u4ecb\u7ecd\uff0c\u5e0c\u671b\u5bf9\u4f60\u7684\u5b66\u4e60\u6709\u6240\u5e2e\u52a9\uff01\u82e5\u662f\u53d7\u76ca\u532a\u6d45\uff0c\u90a3\u5c31\u52a8\u52a8\u9f20\u6807\u6536\u85cf\u8fd9\u7bc7\u300aphp\u51fd\u6570\u9519\u8bef\u7684\u6392\u67e5\u3001\u5206\u6790\u548c\u89e3\u51b3\u300b\u6587\u7ae0\u5427\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u4e86\u89e3\u76f8\u5173\u6280\u672f\u6587\u7ae0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>php\u51fd\u6570\u9519\u8bef\u7684\u6392\u67e5\u3001\u5206\u6790\u548c\u89e3\u51b3 \u6587\u7ae0\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u300aphp\u51fd\u6570\u9519\u8bef\u7684\u6392\u67e5\u3001\u5206\u6790\u548c\u89e3\u51b3\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 \u8981\u6392\u67e5\u3001\u5206\u6790\u548c\u89e3\u51b3 PHP \u51fd\u6570\u9519\u8bef\uff0c\u8bf7\u9075\u5faa\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u4f7f\u7528 try-catch \u8bed\u53e5\u6355\u83b7\u51fd\u6570\u629b\u51fa\u7684\u5f02\u5e38\u3002\u8bfb\u53d6\u5f02\u5e38\u6d88\u606f\uff0c\u4e86\u89e3\u95ee\u9898\u7684\u6839\u6e90\u3002\u6839\u636e\u5206\u6790\u7ed3\u679c\u4fee\u590d\u4ee3\u7801\uff0c\u786e\u4fdd\u53c2\u6570\u6709\u6548\u4e14\u7b26\u5408\u51fd\u6570\u8981\u6c42\u3002\u8003\u8651\u5728\u51fd\u6570\u4e2d\u6dfb\u52a0\u989d\u5916\u7684\u9a8c\u8bc1\u4ee5\u9632\u6b62\u9519\u8bef\u518d\u6b21\u53d1\u751f\u3002 PHP \u51fd\u6570\u9519\u8bef\u6392\u67e5\u3001\u5206\u6790\u548c\u89e3\u51b3 \u7b80\u4ecb PHP \u51fd\u6570\u9519\u8bef\u662f\u8f6f\u4ef6\u5f00\u53d1\u4e2d\u5e38\u89c1\u7684\u70e6\u607c\u3002\u672c\u6587\u5c06\u901a\u8fc7\u5b9e\u6218\u6848\u4f8b\uff0c\u5f15\u5bfc\u60a8\u5faa\u5e8f\u6e10\u8fdb\u5730\u6392\u67e5\u3001\u5206\u6790\u548c\u89e3\u51b3\u51fd\u6570\u9519\u8bef\uff0c\u4ece\u800c\u63d0\u9ad8\u60a8\u7684 PHP \u7f16\u7a0b\u6280\u80fd\u3002 \u5b9e\u6218\u6848\u4f8b \u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u540d\u4e3a calculateDiscount() \u7684\u51fd\u6570\uff0c\u8be5\u51fd\u6570\u6839\u636e\u7ed9\u5b9a\u7684\u6761\u4ef6\u8ba1\u7b97\u6298\u6263\u3002 function calculateDiscount($subtotal, $discountPercentage) { if (!is_numeric($subtotal)) { throw new InvalidArgumentException(&#8220;Subtotal must be a number.&#8221;); } elseif (!is_numeric($discountPercentage) || $discountPercentage &lt; 0 || $discountPercentage &gt; 1) { throw new InvalidArgumentException(&#8220;Discount percentage must be a number between 0 and [&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-48266","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48266","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=48266"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48266\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}