{"id":47314,"date":"2024-12-02T10:10:03","date_gmt":"2024-12-02T02:10:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47314\/"},"modified":"2024-12-02T10:10:03","modified_gmt":"2024-12-02T02:10:03","slug":"php-%e5%87%bd%e6%95%b0%e4%b8%ad%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e9%80%92%e5%bd%92%e6%9d%a5%e6%a8%a1%e6%8b%9f%e6%8e%b7%e9%aa%b0%e5%ad%90%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47314\/","title":{"rendered":"PHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u6a21\u62df\u63b7\u9ab0\u5b50\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u6a21\u62df\u63b7\u9ab0\u5b50\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\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u6a21\u62df\u63b7\u9ab0\u5b50\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>PHP \u4e2d\u5229\u7528\u9012\u5f52\u6a21\u62df\u63b7\u9ab0\u5b50\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa rollDice() \u51fd\u6570\uff0c\u6839\u636e\u9ab0\u5b50\u7684\u8fb9\u6570\u751f\u6210\u968f\u673a\u6570\u5b57\uff0c\u5e76\u5728\u5f97\u5230\u9ab0\u5b50\u7684\u6700\u9ad8\u9762\u6570\u65f6\u9012\u5f52\u8c03\u7528\u81ea\u8eab\uff0c\u6700\u7ec8\u8fd4\u56de\u6a21\u62df\u7684\u9ab0\u5b50\u503c\u3002\u5b9e\u6218\u6848\u4f8b\u4e2d\uff0c\u4ee3\u7801\u6a21\u62df\u63b7\u5341\u6b21\u516d\u9762\u9ab0\u5b50\uff0c\u8f93\u51fa\u63b7\u9ab0\u5b50\u7684\u7ed3\u679c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729910487671c56d7e60f8.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u6a21\u62df\u63b7\u9ab0\u5b50\uff1f\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u6a21\u62df\u63b7\u9ab0\u5b50\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u4e2d\u5229\u7528\u9012\u5f52\u6a21\u62df\u63b7\u9ab0\u5b50<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u9012\u5f52\u662f\u4e00\u79cd\u7f16\u7a0b\u6280\u672f\uff0c\u5b83\u5141\u8bb8\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u3002\u5728\u6a21\u62df\u63b7\u9ab0\u5b50\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u6765\u521b\u5efa\u6bcf\u4e2a\u63b7\u9ab0\u5b50\u7684\u968f\u673a\u7ed3\u679c\u3002\u672c\u6587\u5c06\u5c55\u793a\u5982\u4f55\u4f7f\u7528 PHP \u4e2d\u7684\u9012\u5f52\u6765\u6a21\u62df\u63b7\u9ab0\u5b50\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\u3002<\/p>\n<p><strong>\u521b\u5efa\u9ab0\u5b50\u51fd\u6570<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <code>rollDice()<\/code> \u7684\u51fd\u6570\u3002\u6b64\u51fd\u6570\u5c06\u4f7f\u7528\u9012\u5f52\u6a21\u62df\u63b7\u9ab0\u5b50\u5e76\u8fd4\u56de\u4e00\u4e2a\u968f\u673a\u6570\u5b57\uff1a<\/p>\n<pre>function rollDice(int $sides = 6): int\n{\n    if ($sides &lt; 1) {\n        throw new InvalidArgumentException(\"Invalid number of sides: $sides\");\n    }\n\n    $result = rand(1, $sides);\n\n    if ($result == $sides) {\n        $result += rollDice($sides);\n    }\n\n    return $result;\n}<\/pre>\n<p><strong>\u51fd\u6570\u8bf4\u660e<\/strong><\/p>\n<ul>\n<li><code>$sides<\/code> \u53c2\u6570\u6307\u5b9a\u9ab0\u5b50\u7684\u8fb9\u6570\u3002<\/li>\n<li>\u5982\u679c <code>$sides<\/code> \u5c0f\u4e8e 1\uff0c\u51fd\u6570\u4f1a\u5f15\u53d1\u5f02\u5e38\u3002<\/li>\n<li><code>rand(1, $sides)<\/code> \u751f\u6210\u4e00\u4e2a\u4ecb\u4e8e 1 \u548c <code>$sides<\/code> \u4e4b\u95f4\u7684\u968f\u673a\u6570\u5b57\u3002<\/li>\n<li>\u5982\u679c\u751f\u6210\u7684\u6570\u5b57\u662f <code>$sides<\/code>\uff08\u9ab0\u5b50\u7684\u6700\u9ad8\u9762\u6570\uff09\uff0c\u9012\u5f52\u8c03\u7528 <code>rollDice()<\/code> \u5e76\u5c06\u7ed3\u679c\u6dfb\u52a0\u5230\u521d\u59cb\u7ed3\u679c\u4e2d\u3002<\/li>\n<li>\u6700\u7ec8\u7ed3\u679c\u8fd4\u56de\u6a21\u62df\u7684\u9ab0\u5b50\u503c\u3002<\/li>\n<\/ul>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u8ba9\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u6765\u6f14\u793a <code>rollDice()<\/code> \u51fd\u6570\uff1a<\/p>\n<pre>\/\/ \u6a21\u62df\u63b7\u4e00\u4e2a\u516d\u9762\u9ab0\u5b50 10 \u6b21\nfor ($i = 1; $i &lt;= 10; $i++) {\n    $roll = rollDice();\n    echo \"\u63b7\u7b2c $i \u6b21: $roll\\n\";\n}<\/pre>\n<p><strong>\u8fd0\u884c\u7ed3\u679c<\/strong><\/p>\n<p>\u8be5\u4ee3\u7801\u4f1a\u8f93\u51fa\u7c7b\u4f3c\u4ee5\u4e0b\u7ed3\u679c\uff1a<\/p>\n<pre>\u63b7\u7b2c 1 \u6b21: 2\n\u63b7\u7b2c 2 \u6b21: 5\n\u63b7\u7b2c 3 \u6b21: 6\n\u63b7\u7b2c 4 \u6b21: 4\n\u63b7\u7b2c 5 \u6b21: 3\n\u63b7\u7b2c 6 \u6b21: 5\n\u63b7\u7b2c 7 \u6b21: 1\n\u63b7\u7b2c 8 \u6b21: 6\n\u63b7\u7b2c 9 \u6b21: 4\n\u63b7\u7b2c 10 \u6b21: 3<\/pre>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6587\u7ae0\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u6a21\u62df\u63b7\u9ab0\u5b50\uff1f \u5927\u5bb6\u597d\uff0c\u6211\u4eec\u53c8\u89c1\u9762\u4e86\u554a~\u672c\u6587\u300aPHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u6a21\u62df\u63b7\u9ab0\u5b50\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~ PHP \u4e2d\u5229\u7528\u9012\u5f52\u6a21\u62df\u63b7\u9ab0\u5b50\uff0c\u53ef\u4ee5\u901a\u8fc7\u521b\u5efa rollDice() \u51fd\u6570\uff0c\u6839\u636e\u9ab0\u5b50\u7684\u8fb9\u6570\u751f\u6210\u968f\u673a\u6570\u5b57\uff0c\u5e76\u5728\u5f97\u5230\u9ab0\u5b50\u7684\u6700\u9ad8\u9762\u6570\u65f6\u9012\u5f52\u8c03\u7528\u81ea\u8eab\uff0c\u6700\u7ec8\u8fd4\u56de\u6a21\u62df\u7684\u9ab0\u5b50\u503c\u3002\u5b9e\u6218\u6848\u4f8b\u4e2d\uff0c\u4ee3\u7801\u6a21\u62df\u63b7\u5341\u6b21\u516d\u9762\u9ab0\u5b50\uff0c\u8f93\u51fa\u63b7\u9ab0\u5b50\u7684\u7ed3\u679c\u3002 PHP \u4e2d\u5229\u7528\u9012\u5f52\u6a21\u62df\u63b7\u9ab0\u5b50 \u7b80\u4ecb \u9012\u5f52\u662f\u4e00\u79cd\u7f16\u7a0b\u6280\u672f\uff0c\u5b83\u5141\u8bb8\u51fd\u6570\u8c03\u7528\u81ea\u8eab\u3002\u5728\u6a21\u62df\u63b7\u9ab0\u5b50\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u9012\u5f52\u6765\u521b\u5efa\u6bcf\u4e2a\u63b7\u9ab0\u5b50\u7684\u968f\u673a\u7ed3\u679c\u3002\u672c\u6587\u5c06\u5c55\u793a\u5982\u4f55\u4f7f\u7528 PHP \u4e2d\u7684\u9012\u5f52\u6765\u6a21\u62df\u63b7\u9ab0\u5b50\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5b9e\u6218\u6848\u4f8b\u3002 \u521b\u5efa\u9ab0\u5b50\u51fd\u6570 \u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a rollDice() \u7684\u51fd\u6570\u3002\u6b64\u51fd\u6570\u5c06\u4f7f\u7528\u9012\u5f52\u6a21\u62df\u63b7\u9ab0\u5b50\u5e76\u8fd4\u56de\u4e00\u4e2a\u968f\u673a\u6570\u5b57\uff1a function rollDice(int $sides = 6): int { if ($sides &lt; 1) { throw new InvalidArgumentException(&#8220;Invalid number of sides: $sides&#8221;); } $result = rand(1, $sides); if ($result == $sides) { $result += rollDice($sides); } return $result; } \u51fd\u6570\u8bf4\u660e [&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-47314","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47314","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=47314"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47314\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}