{"id":47573,"date":"2024-12-02T17:13:37","date_gmt":"2024-12-02T09:13:37","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47573\/"},"modified":"2024-12-02T17:13:37","modified_gmt":"2024-12-02T09:13:37","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%e7%94%9f%e6%88%90%e9%9a%8f%e6%9c%ba%e6%95%b0%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47573\/","title":{"rendered":"PHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u751f\u6210\u968f\u673a\u6570\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u751f\u6210\u968f\u673a\u6570\uff1f<\/h1>\n<p>\u76ee\u524d\u7c73\u4e91\u4e0a\u5df2\u7ecf\u6709\u5f88\u591a\u5173\u4e8e<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u7684\u6587\u7ae0\u4e86\uff0c\u81ea\u5df1\u5728\u521d\u6b21\u9605\u8bfb\u8fd9\u4e9b\u6587\u7ae0\u4e2d\uff0c\u4e5f\u89c1\u8bc6\u5230\u4e86\u5f88\u591a\u5b66\u4e60\u601d\u8def\uff1b\u90a3\u4e48\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\u751f\u6210\u968f\u673a\u6570\uff1f\u300b<\/span>\uff0c\u4e5f\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff0c\u5982\u679c\u9605\u8bfb\u5b8c\u540e\u771f\u7684\u5bf9\u4f60\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u6709\u5e2e\u52a9\uff0c\u6b22\u8fce\u52a8\u52a8\u624b\u6307\uff0c\u8bc4\u8bba\u7559\u8a00\u5e76\u5206\u4eab~<\/p>\n<p>\u901a\u8fc7\u9012\u5f52\u8c03\u7528\uff0cPHP \u51fd\u6570 generateRandom() \u53ef\u751f\u6210\u4f2a\u968f\u673a\u6570\u5e8f\u5217\uff0c\u5176\u8303\u56f4\u5728 min \u548c max \u4e4b\u95f4\u3002\u51fd\u6570\u6301\u7eed\u9012\u5f52\uff0c\u76f4\u81f3\u8fbe\u5230\u6700\u5927\u503c\uff0c\u7136\u540e\u8fd4\u56de\u968f\u673a\u6784\u5efa\u7684\u968f\u673a\u6570\u5e8f\u5217\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729924102671c8c06eef02.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u751f\u6210\u968f\u673a\u6570\uff1f\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u751f\u6210\u968f\u673a\u6570\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u4e2d\u7684\u9012\u5f52\u751f\u6210\u968f\u673a\u6570<\/strong><\/p>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u9012\u5f52\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7f16\u7a0b\u6280\u672f\uff0c\u5141\u8bb8\u51fd\u6570\u91cd\u590d\u8c03\u7528\u81ea\u8eab\u3002\u5728\u751f\u6210\u968f\u673a\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u9012\u5f52\u6765\u521b\u5efa\u590d\u6742\u7684\u968f\u673a\u6570\u5e8f\u5217\u3002<\/p>\n<p><strong>\u4ee3\u7801\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b PHP \u51fd\u6570\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u751f\u6210\u968f\u673a\u6570\uff1a<\/p>\n<pre>function generateRandom($min, $max) {\n  \/\/ \u57fa\u7ebf\u6761\u4ef6\uff1a\u5f53\u8fbe\u5230\u6700\u5927\u503c\u65f6\u505c\u6b62\u9012\u5f52\n  if ($min &gt; $max) {\n    return $max;\n  }\n\n  \/\/ \u4ea7\u751f\u968f\u673a\u6570\n  $random = rand($min, $max);\n\n  \/\/ \u9012\u5f52\u8c03\u7528\uff0c\u4ee5\u751f\u6210\u53e6\u4e00\u4e2a\u968f\u673a\u6570\n  return $random . generateRandom($min, $max);\n}<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b64\u51fd\u6570\u6765\u751f\u6210\u4e00\u4e2a\u5305\u542b 1 \u5230 99 \u4e4b\u95f4 10 \u4e2a\u552f\u4e00\u968f\u673a\u6570\u7684\u6570\u7ec4\uff1a<\/p>\n<pre>$result = [];\n\nfor ($i = 0; $i &lt; 10; $i++) {\n  $result[] = generateRandom(1, 99);\n}\n\nprint_r($result);<\/pre>\n<p><strong>\u8fd0\u884c\u7ed3\u679c<\/strong><\/p>\n<p>\u6b64\u811a\u672c\u5c06\u8f93\u51fa\u4e00\u4e2a\u5305\u542b 10 \u4e2a\u552f\u4e00\u968f\u673a\u6570\u7684\u6570\u7ec4\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>[16, 32, 83, 56, 92, 75, 43, 91, 68, 21]<\/pre>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u4f7f\u7528\u9012\u5f52\u53ef\u4ee5\u8f7b\u677e\u751f\u6210\u590d\u6742\u4e14\u4f2a\u968f\u673a\u7684\u6570\u5b57\u5e8f\u5217\u3002\u901a\u8fc7\u8c03\u6574 <code>min<\/code> \u548c <code>max<\/code> \u53c2\u6570\uff0c\u60a8\u53ef\u4ee5\u63a7\u5236\u968f\u673a\u6570\u8303\u56f4\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u751f\u6210\u968f\u673a\u6570\uff1f \u76ee\u524d\u7c73\u4e91\u4e0a\u5df2\u7ecf\u6709\u5f88\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u6587\u7ae0\u4e86\uff0c\u81ea\u5df1\u5728\u521d\u6b21\u9605\u8bfb\u8fd9\u4e9b\u6587\u7ae0\u4e2d\uff0c\u4e5f\u89c1\u8bc6\u5230\u4e86\u5f88\u591a\u5b66\u4e60\u601d\u8def\uff1b\u90a3\u4e48\u672c\u6587\u300aPHP \u51fd\u6570\u4e2d\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u6765\u751f\u6210\u968f\u673a\u6570\uff1f\u300b\uff0c\u4e5f\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff0c\u5982\u679c\u9605\u8bfb\u5b8c\u540e\u771f\u7684\u5bf9\u4f60\u5b66\u4e60\u6587\u7ae0\u6709\u5e2e\u52a9\uff0c\u6b22\u8fce\u52a8\u52a8\u624b\u6307\uff0c\u8bc4\u8bba\u7559\u8a00\u5e76\u5206\u4eab~ \u901a\u8fc7\u9012\u5f52\u8c03\u7528\uff0cPHP \u51fd\u6570 generateRandom() \u53ef\u751f\u6210\u4f2a\u968f\u673a\u6570\u5e8f\u5217\uff0c\u5176\u8303\u56f4\u5728 min \u548c max \u4e4b\u95f4\u3002\u51fd\u6570\u6301\u7eed\u9012\u5f52\uff0c\u76f4\u81f3\u8fbe\u5230\u6700\u5927\u503c\uff0c\u7136\u540e\u8fd4\u56de\u968f\u673a\u6784\u5efa\u7684\u968f\u673a\u6570\u5e8f\u5217\u3002 \u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u4e2d\u7684\u9012\u5f52\u751f\u6210\u968f\u673a\u6570 \u7b80\u4ecb \u9012\u5f52\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u7f16\u7a0b\u6280\u672f\uff0c\u5141\u8bb8\u51fd\u6570\u91cd\u590d\u8c03\u7528\u81ea\u8eab\u3002\u5728\u751f\u6210\u968f\u673a\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u9012\u5f52\u6765\u521b\u5efa\u590d\u6742\u7684\u968f\u673a\u6570\u5e8f\u5217\u3002 \u4ee3\u7801\u793a\u4f8b \u4ee5\u4e0b PHP \u51fd\u6570\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u9012\u5f52\u751f\u6210\u968f\u673a\u6570\uff1a function generateRandom($min, $max) { \/\/ \u57fa\u7ebf\u6761\u4ef6\uff1a\u5f53\u8fbe\u5230\u6700\u5927\u503c\u65f6\u505c\u6b62\u9012\u5f52 if ($min &gt; $max) { return $max; } \/\/ \u4ea7\u751f\u968f\u673a\u6570 $random = rand($min, $max); \/\/ \u9012\u5f52\u8c03\u7528\uff0c\u4ee5\u751f\u6210\u53e6\u4e00\u4e2a\u968f\u673a\u6570 return $random . generateRandom($min, $max); } \u5b9e\u6218\u6848\u4f8b \u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6b64\u51fd\u6570\u6765\u751f\u6210\u4e00\u4e2a\u5305\u542b 1 \u5230 99 \u4e4b\u95f4 [&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-47573","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47573","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=47573"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47573\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}