{"id":429,"date":"2025-06-10T20:19:49","date_gmt":"2025-06-10T12:19:49","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/jcyy\/429.html"},"modified":"2025-06-10T20:19:49","modified_gmt":"2025-06-10T12:19:49","slug":"%e5%9c%a8php%e4%b8%ad%e4%bd%bf%e7%94%a8%e7%b1%bb%e5%9e%8b%e6%8f%90%e7%a4%ba%ef%bc%9a%e5%85%a8%e9%9d%a2%e6%8c%87%e5%8d%97","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/429.html","title":{"rendered":"\u5728PHP\u4e2d\u4f7f\u7528\u7c7b\u578b\u63d0\u793a\uff1a\u5168\u9762\u6307\u5357"},"content":{"rendered":"<p><body><\/p>\n<h2>&#20171;&#32461;<\/h2>\n<p>PHP&#31867;&#22411;&#25552;&#31034;&#20351;&#24471;&#24320;&#21457;&#20154;&#21592;&#33021;&#22815;&#25351;&#23450;&#20989;&#25968;&#22768;&#26126;&#21644;&#36820;&#22238;&#31867;&#22411;&#20013;&#30340;&#39044;&#26399;&#25968;&#25454;&#31867;&#22411;&#65292;&#20174;&#32780;&#25552;&#39640;&#20195;&#30721;&#30340;&#21487;&#38752;&#24615;&#21644;&#21487;&#35835;&#24615;&#12290;&#26412;&#25351;&#21335;&#20171;&#32461;&#20102;&#22914;&#20309;&#26377;&#25928;&#22320;&#20351;&#29992;PHP&#31867;&#22411;&#25552;&#31034;&#26469;&#20248;&#21270;&#24744;&#30340;&#24212;&#29992;&#31243;&#24207;&#12290;<\/p>\n<h2>&#31867;&#22411;&#25552;&#31034;&#30340;&#22522;&#30784;<\/h2>\n<p>&#31867;&#22411;&#25552;&#31034;&#65292;&#20174;PHP 5&#24320;&#22987;&#24341;&#20837;&#65292;&#20801;&#35768;&#24320;&#21457;&#20154;&#21592;&#22768;&#26126;&#20989;&#25968;&#21442;&#25968;&#30340;&#31867;&#22411;&#12290;&#36825;&#26159;&#19968;&#20010;&#31616;&#21333;&#30340;&#20363;&#23376;&#65306;<\/p>\n<pre><code>function sum(int $a, int $b): int {\n    return $a + $b;\n}\n<\/code><\/pre>\n<p>&#36825;&#27573;&#20195;&#30721;&#29255;&#27573;&#34920;&#26126;&#20102;&#20004;&#32773;&#37117;&#23384;&#22312;&#12290;<code>$a<\/code>and &#26159;&#20013;&#25991;&#20013;&#30340;&ldquo;&#24182;&#19988;&rdquo;&#30340;&#24847;&#24605;&#12290;<code>$b<\/code>&#24517;&#39035;&#26159;&#25972;&#25968;&#65292;&#19988;&#20989;&#25968;<code>sum<\/code>&#24212;&#35813;&#36820;&#22238;&#25972;&#25968;&#12290;<\/p>\n<h2>&#26631;&#37327;&#31867;&#22411;&#22768;&#26126;<\/h2>\n<p>&#22312;PHP 7&#20013;&#65292;&#24341;&#20837;&#20102;&#26631;&#37327;&#31867;&#22411;&#22768;&#26126;&#65292;&#21253;&#25324;&#22914;int&#12289;float&#21644;string&#31561;&#31867;&#22411;&#12290;<code>int<\/code>&#22909;&#30340;&#65292;&#35831;&#21457;&#36865;&#20320;&#38656;&#35201;&#32763;&#35793;&#30340;&#20869;&#23481;&#12290;<code>float<\/code>&#22909;&#30340;&#65292;&#35831;&#25552;&#20379;&#38656;&#35201;&#32763;&#35793;&#30340;&#20869;&#23481;&#12290;<code>string<\/code>&#21644;&#25105;&#26377;&#20160;&#20040;&#20107;&#21527;&#65311;<code>bool<\/code>&#20363;&#22914;&#65306;<\/p>\n<pre><code>function multiply(float $a, float $b): float {\n    return $a * $b;\n}\n<\/code><\/pre>\n<p>&#22312;&#36825;&#20010;&#20363;&#23376;&#20013;&#65292;<code>multiply<\/code>&#26399;&#26395;&#25509;&#25910;&#20004;&#20010;&#28014;&#28857;&#25968;&#24182;&#36820;&#22238;&#19968;&#20010;&#28014;&#28857;&#25968;&#12290;<\/p>\n<h2>&#20005;&#26684;&#30340;&#31867;&#22411;&#26816;&#26597;<\/h2>\n<p>PHP &#26377;&#20004;&#31181;&#31867;&#22411;&#30340;&#26816;&#26597;&#27169;&#24335;&#65306;&#24378;&#21046;&#65288;&#40664;&#35748;&#65289;&#21644;&#20005;&#26684;&#31867;&#22411;&#26816;&#26597;&#27169;&#24335;&#12290;&#35201;&#24378;&#21046;&#20351;&#29992;&#20005;&#26684;&#31867;&#22411;&#65292;&#20320;&#38656;&#35201;&#22768;&#26126;&#31867;&#22411;&#12290;<code>declare(strict_types=1);<\/code>&#22312;&#25991;&#20214;&#30340;&#24320;&#22836;&#12290;&#36825;&#30830;&#20445;&#20102;&#31867;&#22411;&#19982;&#25351;&#23450;&#30340;&#19968;&#26679;&#65306;<\/p>\n<pre><code>declare(strict_types=1);\n\nfunction divide(int $a, int $b): float {\n    if ($b === 0) {\n        throw new InvalidArgumentException('Division by zero.');\n    }\n    return $a \/ $b;\n}\n<\/code><\/pre>\n<h2>&#23545;&#35937;&#31867;&#22411;&#22768;&#26126;<\/h2>\n<p>PHP&#20063;&#20801;&#35768;&#25351;&#23450;&#23545;&#35937;&#31867;&#22411;&#12290;&#20197;&#19979;&#26159;&#22914;&#20309;&#20026;&#20551;&#35774;&#30340;&#23454;&#20363;&#36827;&#34892;&#31867;&#22411;&#30340;&#25552;&#31034;&#65306;<code>HttpRequest<\/code>&#22352;&#22909;&#65292;&#20934;&#22791;&#24320;&#22987;&#12290;<\/p>\n<pre><code>function handleRequest(HttpRequest $request): Response {\n    \/\/ process the request\n    return new Response();\n}\n<\/code><\/pre>\n<p>&#36825;&#34920;&#26126;&#20102;<code>handleRequest<\/code>&#26399;&#26395;&#19968;&#20010;&#12290;<code>HttpRequest<\/code>&#23545;&#35937;&#24182;&#23558;&#36820;&#22238;&#19968;&#20010;<code>Response<\/code>&#23545;&#35937;&#12290;<\/p>\n<h2>&#21487;&#36873;&#31867;&#22411;<\/h2>\n<p>&#22312;PHP 7.1&#20013;&#24341;&#20837;&#20102;&#19968;&#20010;&#29305;&#24615;&#65292;&#21487;&#20197;&#25351;&#23450;&#21442;&#25968;&#25110;&#36820;&#22238;&#31867;&#22411;&#20063;&#21487;&#20197;&#26159;&#21487;&#36873;&#30340;&#12290;<code>null<\/code>&#35831;&#25552;&#20379;&#24744;&#38656;&#35201;&#32763;&#35793;&#30340;&#20869;&#23481;&#65292;&#20197;&#20415;&#25105;&#36827;&#34892;&#32763;&#35793;&#12290;<\/p>\n<pre><code>function getUser(int $id): ?User {\n    return findUserById($id) ?: null;\n}\n<\/code><\/pre>\n<p>&#35813;&#20989;&#25968;<code>getUser<\/code>May return a&#8230;<code>User<\/code>&#23545;&#35937;&#12290;<code>null<\/code>&#22914;&#26524;&#25214;&#19981;&#21040;&#29992;&#25143;&#12290;<\/p>\n<h2>void &#36820;&#22238;&#31867;&#22411;<\/h2>\n<p>PHP 7.1&#20063;&#24341;&#20837;&#20102;&#26032;&#30340;&#29305;&#24615;&#12290;<code>void<\/code>&#36820;&#22238;&#31867;&#22411;&#65292;&#34920;&#31034;&#35813;&#20989;&#25968;&#19981;&#36820;&#22238;&#20219;&#20309;&#20540;&#65306;<\/p>\n<pre><code>function logMessage(string $message): void {\n    file_put_contents('log.txt', $message);\n    \/\/ no return statement\n}\n<\/code><\/pre>\n<h2>&#26041;&#27861;&#22768;&#26126;&#20013;&#30340;&#31867;&#22411;&#35828;&#26126;<\/h2>\n<p>&#31867;&#22411;&#22768;&#26126;&#22312;&#31867;&#26041;&#27861;&#20013;&#20063;&#31867;&#20284;&#12290;&#36825;&#37324;&#26377;&#19968;&#20010;&#20351;&#29992;&#31867;&#30340;&#20363;&#23376;&#65306;<\/p>\n<pre><code>class Calculator {\n    public function add(int $a, int $b): int {\n        return $a + $b;\n    }\n}\n<\/code><\/pre>\n<p>&#29677;&#32423;&#12290;<code>Calculator<\/code>&#26377;&#26041;&#27861;&#12290;<code>add<\/code>&#37027;&#30830;&#20445;&#20102;&#12290;<code>$a<\/code>and &#26159;&#20013;&#25991;&#20013;&#30340;&ldquo;&#24182;&#19988;&rdquo;&#25110;&ldquo;&#32780;&#19988;&rdquo;&#65292;&#29992;&#20110;&#36830;&#25509;&#20004;&#20010;&#24182;&#21015;&#30340;&#21477;&#23376;&#65292;&#34920;&#31034;&#21516;&#26102;&#23384;&#22312;&#30340;&#20107;&#23454;&#25110;&#24773;&#20917;&#12290;<code>$b<\/code>&#25972;&#25968;&#26159;&#23454;&#25968;&#30340;&#19968;&#31181;&#12290;<\/p>\n<h2>&#20351;&#29992;&#31867;&#22411;&#25552;&#31034;&#19982;&#25968;&#32452;<\/h2>\n<p>&#25968;&#32452;&#31867;&#22411;&#25552;&#31034;&#30830;&#20445;&#20989;&#25968;&#20165;&#25509;&#25910;&#25968;&#32452;&#20316;&#20026;&#20854;&#21442;&#25968;&#65306;<\/p>\n<pre><code>function setValues(array $values): void {\n    foreach ($values as $key =&gt; $value) {\n        \/\/ Set each value\n    }\n}\n<\/code><\/pre>\n<h2>&#36845;&#20195;&#22120;&#21644;&#21487;&#35843;&#29992;&#31867;&#22411;<\/h2>\n<p>PHP 7.1 &#24341;&#20837;&#20102;&#12290;<code>iterable<\/code>&#31867;&#22411;&#22768;&#26126;&#20801;&#35768;&#25968;&#32452;&#21644;&#23454;&#20363;&#21270;&#23545;&#35937;&#30340;&#24341;&#29992;&#12290;<code>Traversable<\/code>&#21516;&#26679;&#65292;&#20063;&#26159;&#36825;&#26679;&#12290;<code>callable<\/code>&#20801;&#35768;&#21442;&#25968;&#20026;&#21487;&#35843;&#29992;&#20989;&#25968;&#65306;<\/p>\n<pre><code>function process(iterable $items, callable $callback): void {\n    foreach ($items as $item) {\n        $callback($item);\n    }\n}\n<\/code><\/pre>\n<h2>&#25151;&#20135;&#31867;&#22411;&#25552;&#31034;<\/h2>\n<p>&#22312;PHP 7.4&#29256;&#26412;&#20013;&#65292;&#24341;&#20837;&#20102;&#23646;&#24615;&#31867;&#22411;&#25552;&#31034;&#21151;&#33021;&#65292;&#20801;&#35768;&#31867;&#30340;&#23646;&#24615;&#20855;&#26377;&#31867;&#22411;&#22768;&#26126;&#65306;<\/p>\n<pre><code>class User {\n    public int $id;\n    public string $name;\n}\n<\/code><\/pre>\n<h2>&#22312;PHP 8&#20013;&#65292;&#24341;&#20837;&#20102;&#32852;&#21512;&#31867;&#22411;&#65288;Union Types&#65289;&#12290;<\/h2>\n<p>PHP 8 &#24341;&#20837;&#20102;&#32852;&#21512;&#31867;&#22411;&#65292;&#20801;&#35768;&#20320;&#22768;&#26126;&#19968;&#20010;&#21464;&#37327;&#21487;&#20197;&#23384;&#20648;&#22810;&#31181;&#31867;&#22411;&#30340;&#20540;&#12290;&#36825;&#26159;&#19968;&#20010;&#31034;&#20363;&#65306;<\/p>\n<pre><code>function debugInfo(int|string|bool $data): void {\n    var_dump($data);\n}\n<\/code><\/pre>\n<h2>&#39640;&#32423;&#31867;&#22411;&#25552;&#31034;&#65306;&#38745;&#24577;&#20998;&#26512;&#19982;&#27867;&#22411;<\/h2>\n<p>PHP&#26412;&#36523;&#19981;&#25903;&#25345;&#27867;&#22411;&#65292;&#20294;&#20320;&#21487;&#20197;&#36890;&#36807;&#32467;&#21512;&#20351;&#29992;PHPDoc&#27880;&#37322;&#21644;&#38745;&#24577;&#20998;&#26512;&#24037;&#20855;&#26469;&#23454;&#29616;&#31867;&#20284;&#27867;&#22411;&#30340;&#34892;&#20026;&#65292;&#36825;&#20123;&#24037;&#20855;&#21487;&#20197;&#24110;&#21161;&#26356;&#22909;&#22320;&#29702;&#35299;&#20195;&#30721;&#65306;<\/p>\n<pre><code>\/**\n * @template T\n * @param T $item\n * @return T\n *\/\nfunction duplicate($item) {\n    return $item;\n}\n<\/code><\/pre>\n<h2>&#26368;&#20339;&#23454;&#36341;&#65306;&#20351;&#29992;&#31867;&#22411;&#25552;&#31034;<\/h2>\n<p>&#35831;&#30830;&#20445;&#24744;&#30340;PHP&#29256;&#26412;&#20445;&#25345;&#26368;&#26032;&#65292;&#20197;&#21033;&#29992;&#26368;&#26032;&#30340;&#31867;&#22411;&#25552;&#31034;&#21151;&#33021;&#12290;<\/p>\n<p>&#22312;&#24744;&#30340;&#39033;&#30446;&#20013;&#19968;&#33268;&#20351;&#29992;&#31867;&#22411;&#25552;&#31034;&#20197;&#25552;&#39640;&#21487;&#35835;&#24615;&#21644;&#32500;&#25252;&#24615;&#12290;<\/p>\n<p>&#25552;&#20513;&#20005;&#26684;&#30340;&#31867;&#22411;&#26816;&#26597;&#65292;&#23588;&#20854;&#26159;&#22312;&#22823;&#22411;&#20195;&#30721;&#24211;&#20013;&#65292;&#20197;&#38450;&#27490;&#32454;&#24494;&#30340;&#38169;&#35823;&#12290;<\/p>\n<p>&#22312;&#21487;&#33021;&#30340;&#24773;&#20917;&#19979;&#65292;&#22987;&#32456;&#20351;&#29992;&#31867;&#22411;&#25552;&#31034;&#20197;&#25552;&#39640;&#20195;&#30721;&#36136;&#37327;&#12290;<\/p>\n<h2>&#32467;&#35770;&#12290;<\/h2>\n<p>&#22312;PHP&#20013;&#20351;&#29992;&#31867;&#22411;&#25552;&#31034;&#26377;&#21161;&#20110;&#21019;&#24314;&#26356;&#24178;&#20928;&#12289;&#26356;&#21487;&#38752;&#21644;&#26356;&#20855;&#21487;&#32500;&#25252;&#24615;&#30340;&#20195;&#30721;&#12290;&#36890;&#36807;&#36981;&#24490;&#36825;&#37324;&#21015;&#20986;&#30340;&#24314;&#35758;&#21644;&#23454;&#36341;&#65292;PHP&#24320;&#21457;&#20154;&#21592;&#21487;&#20197;&#21033;&#29992;&#31867;&#22411;&#25552;&#31034;&#30340;&#21147;&#37327;&#26469;&#20943;&#23569;&#38169;&#35823;&#24182;&#25552;&#39640;&#20195;&#30721;&#36136;&#37327;&#12290;<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#20171;&#32461; PHP&#31867;&#22411;&#25552;&#31034;&#20351;&#24471;&#24320;&#21457;&#20154;&#21592;&#33021;&#22815;&#25351;&#23450;&#20989;&#25968;&#22768;&#038;#26..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[38],"tuisongtax":[],"class_list":["post-429","my1js","type-my1js","status-publish","hentry","my1js2nav-jcyy"],"acf":{"qian_art_seotitle":"","qian_art_seotitle_source":{"label":"SEO\u6807\u9898","type":"text","formatted_value":""},"qian_art_seokws":"","qian_art_seokws_source":{"label":"SEO\u5173\u952e\u8bcd","type":"text","formatted_value":""},"qian_art_stzhong":"","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":""}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js\/429","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/types\/my1js"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/users\/1"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/media?parent=429"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=429"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=429"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=429"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}