{"id":47942,"date":"2024-12-02T13:21:02","date_gmt":"2024-12-02T05:21:02","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47942\/"},"modified":"2024-12-02T13:21:02","modified_gmt":"2024-12-02T05:21:02","slug":"php-%e5%87%bd%e6%95%b0%e4%b8%ad%e4%bd%bf%e7%94%a8%e5%8f%98%e9%87%8f%e7%b1%bb%e5%9e%8b%e6%97%b6%e9%9c%80%e8%a6%81%e6%b3%a8%e6%84%8f%e5%93%aa%e4%ba%9b%e4%ba%8b%e9%a1%b9%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47942\/","title":{"rendered":"PHP \u51fd\u6570\u4e2d\u4f7f\u7528\u53d8\u91cf\u7c7b\u578b\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u4e2d\u4f7f\u7528\u53d8\u91cf\u7c7b\u578b\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/h1>\n<p>\u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662f\u7c73\u4e91\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u4e2d\u4f7f\u7528\u53d8\u91cf\u7c7b\u578b\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f\u300b<\/span>\uff0c\u672c\u6587\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\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<p>PHP \u51fd\u6570\u7c7b\u578b\u63d0\u793a\u6ce8\u610f\u4e8b\u9879\uff1a\u4e3a\u53c2\u6570\u548c\u8fd4\u56de\u503c\u6307\u5b9a\u671f\u671b\u7c7b\u578b\uff0c\u63d0\u9ad8\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u6307\u5b9a\u975e\u6574\u6570\u53c2\u6570\u4f1a\u629b\u51fa TypeError \u5f02\u5e38\u3002\u53ef\u4f7f\u7528\u9ed8\u8ba4\u503c\u548c null \u5408\u5e76\u8fd0\u7b97\u7b26\u4e3a\u53c2\u6570\u6307\u5b9a\u9ed8\u8ba4\u503c\u3002\u53ef\u5728 PHP 8.0 \u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\u4f7f\u7528 ? \u8fd0\u7b97\u7b26\u6307\u5b9a\u53ef\u9009\u53c2\u6570\u3002\u9664\u53c2\u6570\u7c7b\u578b\u5916\uff0c\u8fd8\u53ef\u6307\u5b9a\u8fd4\u56de\u503c\u7c7b\u578b\uff0c\u907f\u514d\u610f\u5916\u7c7b\u578b\u8fd4\u56de\u3002\u6ce8\u610f\u65e0\u6548\u7c7b\u578b\uff1amixed \u8868\u793a\u4efb\u4f55\u7c7b\u578b\uff0cvoid \u8868\u793a\u65e0\u8fd4\u56de\u503c\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729944804671cdce4b5737.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u4e2d\u4f7f\u7528\u53d8\u91cf\u7c7b\u578b\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u4e2d\u4f7f\u7528\u53d8\u91cf\u7c7b\u578b\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5728 PHP \u51fd\u6570\u4e2d\u4f7f\u7528\u53d8\u91cf\u7c7b\u578b\u65f6\u9700\u8981\u6ce8\u610f\u7684\u4e8b\u9879<\/strong><\/p>\n<p><strong>\u7c7b\u578b\u63d0\u793a<\/strong><\/p>\n<p>PHP 7.0 \u5f15\u5165\u4e86\u7c7b\u578b\u63d0\u793a\uff0c\u5141\u8bb8\u60a8\u6307\u5b9a\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7684\u671f\u671b\u7c7b\u578b\u3002\u5b83\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u5e76\u6709\u52a9\u4e8e\u51cf\u8f7b\u7c7b\u578b\u68c0\u67e5\u7684\u5f00\u9500\u3002<\/p>\n<pre>function sum(int $a, int $b): int {\n    return $a + $b;\n}<\/pre>\n<p><strong>\u5b9e\u9645\u6848\u4f8b<\/strong><\/p>\n<p>\u8003\u8651\u4ee5\u4e0b\u51fd\u6570\uff0c\u7528\u4e8e\u8ba1\u7b97\u4e24\u4e2a\u6574\u6570\u7684\u603b\u548c\uff1a<\/p>\n<pre>function add($a, $b) {\n    return $a + $b;\n}<\/pre>\n<p>\u5982\u679c\u4f20\u5165\u975e\u6574\u6570\u53c2\u6570\uff0c\u6b64\u51fd\u6570\u53ef\u80fd\u4f1a\u5f15\u53d1\u9519\u8bef\u3002\u4f7f\u7528\u7c7b\u578b\u63d0\u793a\u53ef\u4ee5\u89e3\u51b3\u6b64\u95ee\u9898\uff1a<\/p>\n<pre>function add(int $a, int $b): int {\n    return $a + $b;\n}<\/pre>\n<p>\u73b0\u5728\uff0c\u5982\u679c\u5c1d\u8bd5\u5411\u6b64\u51fd\u6570\u4f20\u9012\u975e\u6574\u6570\u53c2\u6570\uff0c\u5b83\u5c06\u629b\u51fa\u4e00\u4e2a TypeError \u5f02\u5e38\u3002<\/p>\n<p><strong>\u9ed8\u8ba4\u503c<\/strong><\/p>\n<p>\u6709\u65f6\u60a8\u53ef\u80fd\u5e0c\u671b\u4e3a\u53c2\u6570\u6307\u5b9a\u9ed8\u8ba4\u503c\u3002\u53ef\u4ee5\u4f7f\u7528\u7c7b\u578b\u63d0\u793a\u548c null \u5408\u5e76\u8fd0\u7b97\u7b26\u6765\u5b9e\u73b0\uff1a<\/p>\n<pre>function greeting(string $name = null): string {\n    return $name ?? 'World';\n}<\/pre>\n<p><strong>\u53ef\u9009\u53c2\u6570<\/strong><\/p>\n<p>\u5728 PHP 8.0 \u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>?<\/code> \u8fd0\u7b97\u7b26\u6307\u5b9a\u53ef\u9009\u53c2\u6570\uff1a<\/p>\n<pre>function greeting(?string $name): string {\n    return $name ?? 'World';\n}<\/pre>\n<p><strong>\u8fd4\u56de\u503c\u7c7b\u578b<\/strong><\/p>\n<p>\u9664\u4e86\u53c2\u6570\u7c7b\u578b\uff0c\u60a8\u8fd8\u53ef\u4ee5\u6307\u5b9a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u7c7b\u578b\u3002\u8fd9\u6709\u52a9\u4e8e\u6587\u6863\u5316\u51fd\u6570\u5e76\u907f\u514d\u8fd4\u56de\u610f\u5916\u7c7b\u578b\u3002<\/p>\n<pre>function findMax(int $a, int $b): int {\n    return $a &gt; $b ? $a : $b;\n}<\/pre>\n<p><strong>\u65e0\u6548\u7c7b\u578b<\/strong><\/p>\n<p>PHP 7.0 \u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\u4e0d\u53ef\u7528\u7684\u4e00\u4e9b\u65e0\u6548\u7c7b\u578b\uff1a<\/p>\n<ul>\n<li><code>mixed<\/code>\uff1a\u8868\u793a\u4efb\u4f55\u7c7b\u578b\u503c\u3002<\/li>\n<li><code>void<\/code>\uff1a\u8868\u793a\u51fd\u6570\u4e0d\u8fd4\u56de\u4efb\u4f55\u503c\u3002<\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd\u7c7b\u578b\u63d0\u793a\u4e0e\u51fd\u6570\u7684\u5b9e\u9645\u5b9e\u73b0\u76f8\u5339\u914d\u3002<\/li>\n<li>\u8003\u8651\u5f02\u5e38\u60c5\u51b5\uff0c\u4f8b\u5982\u4f20\u9012\u65e0\u6548\u7c7b\u578b\u7684\u53c2\u6570\u3002<\/li>\n<li>\u4f7f\u7528\u7c7b\u578b\u63d0\u793a\u5e76\u4e0d\u5b8c\u5168\u6d88\u9664\u7c7b\u578b\u68c0\u67e5\u7684\u5f00\u9500\uff0c\u4f46\u53ef\u4ee5\u663e\u7740\u63d0\u9ad8\u6027\u80fd\u3002<\/li>\n<\/ul>\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\u4f7f\u7528\u53d8\u91cf\u7c7b\u578b\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f \u54c8\u55bd\uff01\u5927\u5bb6\u597d\uff0c\u5f88\u9ad8\u5174\u53c8\u89c1\u9762\u4e86\uff0c\u6211\u662f\u7c73\u4e91\u7684\u4e00\u540d\u4f5c\u8005\uff0c\u4eca\u5929\u7531\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u7bc7\u300aPHP \u51fd\u6570\u4e2d\u4f7f\u7528\u53d8\u91cf\u7c7b\u578b\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f\u300b\uff0c\u672c\u6587\u4e3b\u8981\u4f1a\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u5927\u5bb6\u4e00\u8d77\u5b66\u4e60\u8fdb\u6b65\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u3001\u70b9\u8d5e\u3001\u6536\u85cf\u3001\u8f6c\u53d1! \u4e0b\u9762\u5c31\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01 PHP \u51fd\u6570\u7c7b\u578b\u63d0\u793a\u6ce8\u610f\u4e8b\u9879\uff1a\u4e3a\u53c2\u6570\u548c\u8fd4\u56de\u503c\u6307\u5b9a\u671f\u671b\u7c7b\u578b\uff0c\u63d0\u9ad8\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002\u6307\u5b9a\u975e\u6574\u6570\u53c2\u6570\u4f1a\u629b\u51fa TypeError \u5f02\u5e38\u3002\u53ef\u4f7f\u7528\u9ed8\u8ba4\u503c\u548c null \u5408\u5e76\u8fd0\u7b97\u7b26\u4e3a\u53c2\u6570\u6307\u5b9a\u9ed8\u8ba4\u503c\u3002\u53ef\u5728 PHP 8.0 \u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\u4f7f\u7528 ? \u8fd0\u7b97\u7b26\u6307\u5b9a\u53ef\u9009\u53c2\u6570\u3002\u9664\u53c2\u6570\u7c7b\u578b\u5916\uff0c\u8fd8\u53ef\u6307\u5b9a\u8fd4\u56de\u503c\u7c7b\u578b\uff0c\u907f\u514d\u610f\u5916\u7c7b\u578b\u8fd4\u56de\u3002\u6ce8\u610f\u65e0\u6548\u7c7b\u578b\uff1amixed \u8868\u793a\u4efb\u4f55\u7c7b\u578b\uff0cvoid \u8868\u793a\u65e0\u8fd4\u56de\u503c\u3002 \u5728 PHP \u51fd\u6570\u4e2d\u4f7f\u7528\u53d8\u91cf\u7c7b\u578b\u65f6\u9700\u8981\u6ce8\u610f\u7684\u4e8b\u9879 \u7c7b\u578b\u63d0\u793a PHP 7.0 \u5f15\u5165\u4e86\u7c7b\u578b\u63d0\u793a\uff0c\u5141\u8bb8\u60a8\u6307\u5b9a\u51fd\u6570\u53c2\u6570\u548c\u8fd4\u56de\u503c\u7684\u671f\u671b\u7c7b\u578b\u3002\u5b83\u63d0\u9ad8\u4e86\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027\uff0c\u5e76\u6709\u52a9\u4e8e\u51cf\u8f7b\u7c7b\u578b\u68c0\u67e5\u7684\u5f00\u9500\u3002 function sum(int $a, int $b): int { return $a + $b; } \u5b9e\u9645\u6848\u4f8b \u8003\u8651\u4ee5\u4e0b\u51fd\u6570\uff0c\u7528\u4e8e\u8ba1\u7b97\u4e24\u4e2a\u6574\u6570\u7684\u603b\u548c\uff1a function add($a, $b) { return $a + $b; } \u5982\u679c\u4f20\u5165\u975e\u6574\u6570\u53c2\u6570\uff0c\u6b64\u51fd\u6570\u53ef\u80fd\u4f1a\u5f15\u53d1\u9519\u8bef\u3002\u4f7f\u7528\u7c7b\u578b\u63d0\u793a\u53ef\u4ee5\u89e3\u51b3\u6b64\u95ee\u9898\uff1a function add(int $a, int $b): int [&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-47942","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47942","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=47942"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47942\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47942"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}