{"id":426,"date":"2025-06-10T20:16:49","date_gmt":"2025-06-10T12:16:49","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/jcyy\/426.html"},"modified":"2025-06-10T20:16:49","modified_gmt":"2025-06-10T12:16:49","slug":"%e5%9c%a8php%e4%b8%ad%e4%bd%bf%e7%94%a8declare%e6%9e%84%e9%80%a0%e5%8a%9f%e8%83%bd","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/426.html","title":{"rendered":"\u5728PHP\u4e2d\u4f7f\u7528\u2018declare\u2019\u6784\u9020\u529f\u80fd"},"content":{"rendered":"<p><body><\/p>\n<h2>&#20171;&#32461;<\/h2>\n<p>&#29702;&#35299;PHP&#20013;&#30340;&ldquo;declare&rdquo;&#26500;&#36896;&#21487;&#33021;&#19981;&#26159;&#27599;&#20010;PHP&#24320;&#21457;&#32773;&#30340;&#36215;&#28857;&#65292;&#20294;&#23545;&#20854;&#30693;&#35782;&#30340;&#25484;&#25569;&#30830;&#23454;&#20351;&#21021;&#23398;&#32773;&#19982;&#19987;&#23478;&#21306;&#20998;&#24320;&#26469;&#12290; &ldquo;declare&rdquo;&#20851;&#38190;&#23383;&#20801;&#35768;&#35774;&#23450;&#26576;&#20123;&#34892;&#20026;&#65292;&#36825;&#20123;&#34892;&#20026;&#21487;&#20197;&#24433;&#21709;&#33050;&#26412;&#30340;&#25191;&#34892;&#12290;&#26412;&#20837;&#38376;&#23558;&#21521;&#24744;&#20171;&#32461;&#36825;&#20010;&#24378;&#22823;&#30340;&#29305;&#24615;&#65292;&#21253;&#25324;&#20174;&#22522;&#30784;&#35821;&#27861;&#21040;&#39640;&#32423;&#29992;&#20363;&#30340;&#21508;&#31181;&#23454;&#29992;&#31034;&#20363;&#12290;<\/p>\n<h2>&ldquo;&#22768;&#26126;&rdquo;&#26500;&#36896;&#26159;&#20160;&#20040;&#65311;<\/h2>\n<p>&#22312;PHP&#20013;&#65292;&ldquo;&#22768;&#26126;&rdquo;&#26500;&#36896;&#29992;&#20110;&#20026;&#20195;&#30721;&#22359;&#35774;&#32622;&#25191;&#34892;&#25351;&#20196;&#12290;&#23427;&#19981;&#26159;&#20989;&#25968;&#25110;&#20256;&#32479;&#25511;&#21046;&#32467;&#26500;&#22914;&ldquo;if&rdquo;&#25110;&ldquo;while&rdquo;&#12290;&#30456;&#21453;&#65292;&#23427;&#25552;&#20379;&#20102;&#19968;&#31181;&#22312;&#33050;&#26412;&#19968;&#24320;&#22987;&#23601;&#23450;&#21046;PHP&#34892;&#20026;&#30340;&#26041;&#27861;&#65292;&#20363;&#22914;&#28014;&#28857;&#25968;&#30340;&#31934;&#24230;&#25110;&#32534;&#30721;&#22788;&#29702;&#12290;<\/p>\n<pre><code>declare (directive)<\/code><\/pre>\n<p>&#36825;&#26159;&#22522;&#26412;&#30340;&#35821;&#27861;&#65292;&#20854;&#20013;&ldquo;&#25351;&#31034;&rdquo;&#21487;&#20197;&#26159;&#35832;&#22914;&ldquo;tick&rdquo;&#12289;&ldquo;&#32534;&#30721;&rdquo;&#25110;&ldquo;&#20005;&#26684;&#31867;&#22411;&rdquo;&#12290;&#35753;&#25105;&#20204;&#29992;&#31616;&#21333;&#30340;&#20363;&#23376;&#26469;&#35299;&#37322;&#36825;&#20123;&#27010;&#24565;&#65292;&#20197;&#20415;&#26356;&#22909;&#22320;&#29702;&#35299;&#12290;<\/p>\n<h2>&#22768;&#26126;&#30340;&#22522;&#26412;&#29992;&#27861;&#12290;<\/h2>\n<p>&#35753;&#25105;&#20204;&#20174;&#26368;&#31616;&#21333;&#30340;&#20363;&#23376;&#24320;&#22987;&#65306;&ldquo;ticks&rdquo;&#12290;&#19968;&#20010;&ldquo;ticks&rdquo;&#20107;&#20214;&#21457;&#29983;&#22312;PHP&#24341;&#25806;&#30340;&#20302;&#32423;&#25805;&#20316;&#21518;&#25351;&#23450;&#30340;&#25968;&#37327;&#27425;&#12290;&#20351;&#29992;&ldquo;declare&rdquo;&#65292;&#21487;&#20197;&#27599;&#27425;&#21457;&#29983;&ldquo;ticks&rdquo;&#20107;&#20214;&#26102;&#36816;&#34892;&#19968;&#20010;&#20989;&#25968;&#12290;<\/p>\n<pre><code>declare (ticks=1);\n\nregister_tick_function(function() {\n    echo \"Tick emitted.\";\n});\n\nfor ($i = 0; $i &lt; 3; $i++) {\n    \/\/ Code that generates ticks.\n}\n<\/code><\/pre>\n<p>&#22312;&#36825;&#20010;&#20363;&#23376;&#20013;&#65292;&#25105;&#20204;&#23558;&#31995;&#32479;&#35774;&#32622;&#20026;&#27599;&#20302;&#32423;&#25805;&#20316;&#29983;&#25104;&#19968;&#20010;&#20107;&#20214;&#12290;&#28982;&#32780;&#65292;&#38656;&#35201;&#27880;&#24847;&#30340;&#26159;&#65292;&#36807;&#24230;&#20351;&#29992;ticks&#21487;&#33021;&#20250;&#23545;&#24615;&#33021;&#20135;&#29983;&#24433;&#21709;&#65292;&#22240;&#20026;&#36825;&#31181;&#20989;&#25968;&#21487;&#20197;&#39057;&#32321;&#22320;&#34987;&#35843;&#29992;&#12290;<\/p>\n<h2>&#22788;&#29702;&#31934;&#24230;<\/h2>\n<p>&#32771;&#34385;&#19968;&#20010;&#22330;&#26223;&#65292;&#25105;&#20204;&#22788;&#29702;&#30340;&#26159;&#22823;&#22411;&#28014;&#28857;&#25968;&#65292;&#24182;&#38656;&#35201;&#39640;&#31934;&#24230;&#12290;PHP&#25552;&#20379;&#20102;&#22312;&ldquo;declare&rdquo;&#22359;&#20013;&#30340;&ldquo;precision&rdquo;&#25351;&#20196;&#12290;<\/p>\n<pre><code>declare (precision=14);\n\necho 0.333 + 0.666;\n<\/code><\/pre>\n<p>&#36890;&#36807;&#35774;&#32622;&#31934;&#30830;&#24230;&#25351;&#31034;&#31526;&#65292;&#25105;&#20204;&#30340;&#32467;&#26524;&#23558;&#36981;&#24490;&#25351;&#23450;&#30340;&#25968;&#20540;&#31934;&#24230;&#27700;&#24179;&#12290;<\/p>\n<h2>&#23383;&#31526;&#20018;&#32534;&#30721;<\/h2>\n<p>&ldquo;declare&rdquo; &#36824;&#21487;&#20197;&#23450;&#20041;&#33050;&#26412;&#32534;&#30721;&#65292;&#36825;&#35268;&#23450;&#20102;&#33050;&#26412;&#22312;&#20854;&#20013;&#32534;&#30721;&#30340;&#23383;&#31526;&#38598;&#12290;&#36825;&#26377;&#21161;&#20110;&#19981;&#21516;&#24179;&#21488;&#21644;&#22320;&#21306;&#30340;&#20860;&#23481;&#24615;&#12290;<\/p>\n<pre><code>declare(encoding='UTF-8');\n\n\/\/ A UTF-8 encoded script here\n<\/code><\/pre>\n<p>&#29616;&#22312;&#36827;&#20837;&#19968;&#20010;&#26356;&#39640;&#32423;&#19988;&#26497;&#20854;&#24378;&#22823;&#30340;&#25351;&#20196;&#65306;&lsquo;strict_types&rsquo;&#12290;&#36825;&#20250;&#24433;&#21709;PHP&#22914;&#20309;&#22788;&#29702;&#31867;&#22411;&#26816;&#26597;&#12290;<\/p>\n<h2>&#24378;&#21046;&#20351;&#29992;&#20005;&#26684;&#31867;&#22411;<\/h2>\n<p>&#22312;PHP&#20013;&#65292;&#21464;&#37327;&#31867;&#22411;&#36890;&#24120;&#20250;&#33258;&#21160;&#36716;&#25442;&#65288;&#31216;&#20026;&ldquo;&#31867;&#22411;&#20132;&#25442;&rdquo;&#65289;&#12290;&#28982;&#32780;&#65292;&#22312;&#26576;&#20123;&#24773;&#20917;&#19979;&#65292;&#36825;&#21487;&#33021;&#20250;&#23548;&#33268;&#24847;&#22806;&#30340;&#32467;&#26524;&#25110;&#38169;&#35823;&#12290;&#20026;&#36827;&#34892;&#26356;&#20005;&#26684;&#30340;&#31867;&#22411;&#26816;&#26597;&#65292;PHP 7 &#24341;&#20837;&#20102;&lsquo;strict_types&rsquo;&#25351;&#20196;&#65292;&#24433;&#21709;&#21040;&#21442;&#25968;&#31867;&#22411;&#22768;&#26126;&#21644;&#36820;&#22238;&#31867;&#22411;&#22768;&#26126;&#12290;<\/p>\n<pre><code>declare(strict_types=1);\n\nfunction sum(int $a, int $b): int {\n    return $a + $b;\n}\n\necho sum(2, '3'); \/\/ This will raise an error in strict mode\n<\/code><\/pre>\n<p>&#21551;&#29992;&ldquo;&#20005;&#26684;&#31867;&#22411;&rdquo;&#65288;strict_types&#65289;&#26126;&#30830;&#22320;&#24378;&#21270;&#20102;&#24320;&#21457;&#32773;&#24847;&#22270;&#65306;&#24517;&#39035;&#26356;&#21152;&#20005;&#26684;&#22320;&#36981;&#23432;&#31867;&#22411;&#35201;&#27714;&#12290;<\/p>\n<h2>&#22768;&#26126;&#30340;&#33539;&#22260;&#30028;&#23450;<\/h2>\n<p>&#19968;&#20010;&ldquo;&#22768;&#26126;&rdquo;&#22359;&#20165;&#22312;&#20351;&#29992;&#22823;&#25324;&#21495;&#30340;&#24773;&#20917;&#19979;&#23545;&#23427;&#25152;&#21253;&#22260;&#30340;&#20195;&#30721;&#22359;&#29983;&#25928;&#65292;&#21542;&#21017;&#23427;&#20250;&#20316;&#29992;&#20110;&#25972;&#20010;&#25991;&#20214;&#65288;&#25110;&#32773;&#30452;&#21040;&#21478;&#19968;&#20010;&ldquo;&#22768;&#26126;&rdquo;&#22359;&#65289;&#12290;<\/p>\n<pre><code>\/\/ This affects the whole file outside curly braces\ndeclare(strict_types=1);\n\n\/\/ This affects only the enclosed block\ndeclare(strict_types=1) {\n    \/\/ PHP code here\n}\n<\/code><\/pre>\n<h2>&#26465;&#20214;&#22768;&#26126;<\/h2>\n<p>&#20320;&#21487;&#20197;&#29992;&ldquo;declare&rdquo;&#19982;&#26465;&#20214;&#35821;&#21477;&#19968;&#36215;&#20351;&#29992;&#65292;&#36825;&#26679;&#22312;&#22914;&#20309;&#24378;&#21046;&#25191;&#34892;&#26576;&#20123;&#25351;&#31034;&#26102;&#23601;&#33021;&#25552;&#20379;&#24456;&#22823;&#30340;&#28789;&#27963;&#24615;&#12290;<\/p>\n<pre><code>if (PHP_VERSION_ID &gt;= 70000) {\n    declare(strict_types=1);\n}\n<\/code><\/pre>\n<p>&#22312;&#36825;&#20010;&#20363;&#23376;&#20013;&#65292;&#8217;strict_types&#8217; &#21482;&#22312; PHP &#29256;&#26412;&#20026; 7.0.0 &#25110;&#26356;&#39640;&#26102;&#25165;&#21551;&#29992;&#65292;&#23637;&#31034;&#20102; &#8216;declare&#8217; &#21487;&#20197;&#22914;&#20309;&#34987;&#26377;&#31574;&#30053;&#22320;&#20351;&#29992;&#12290;<\/p>\n<h2>&#38519;&#38449;&#21644;&#27880;&#24847;&#20107;&#39033;<\/h2>\n<p>&#35760;&#20303;&#19968;&#28857;&#65306;strict_types &#30452;&#25509;&#20316;&#29992;&#20110;&#25991;&#20214;&#65292;&#19981;&#21463;&#36816;&#34892;&#26102;&#26465;&#20214;&#30340;&#24433;&#21709;&#12290;&#23427;&#24517;&#39035;&#22312;&#25991;&#20214;&#30340;&#26368;&#21069;&#38754;&#22768;&#26126;&#65292;&#21542;&#21017;&#20250;&#23548;&#33268;&#33268;&#21629;&#38169;&#35823;&#12290;<\/p>\n<h2>&#23884;&#22871;&#22768;&#26126;&#35821;&#21477;&#12290;<\/h2>\n<p>&#20320;&#21487;&#20197;&#23884;&#22871;&ldquo;&#22768;&#26126;&rdquo;&#35821;&#21477;&#65292;&#25511;&#21046;&#19981;&#21516;&#20195;&#30721;&#27573;&#22312;&#21516;&#19968;&#32447;&#31243;&#20013;&#30340;&#19981;&#21516;&#26041;&#38754;&#12290;&#24819;&#35937;&#35843;&#25972;tick&#39057;&#29575;&#12289;&#31934;&#24230;&#32423;&#21035;&#21644;&#24378;&#21046;&#25191;&#34892;&#20005;&#26684;&#31867;&#22411;&#65292;&#22312;PHP&#20195;&#30721;&#30340;&#19981;&#21516;&#37096;&#20998;&#23454;&#29616;&#31934;&#30830;&#25511;&#21046;&#12290;<\/p>\n<pre><code>declare(strict_types=1) {\n    \/\/ This code is under strict type checking\n\n    declare(ticks=1) {\n        \/\/ This code will also emit ticks after every statement\n    }\n}\n<\/code><\/pre>\n<h2>&#32467;&#35770;<\/h2>\n<p>&#24635;&#32467;&#26469;&#35828;&#65292;PHP&#20013;&#30340;&ldquo;&#22768;&#26126;&rdquo;&#26500;&#36896;&#26159;&#19968;&#31181;&#37325;&#35201;&#30340;&#20294;&#26377;&#26102;&#34987;&#20302;&#20272;&#30340;&#32452;&#20214;&#65292;&#23427;&#20026;&#24320;&#21457;&#20154;&#21592;&#25552;&#20379;&#20102;&#23545;&#33050;&#26412;&#34892;&#20026;&#36827;&#34892;&#30456;&#24403;&#31243;&#24230;&#25511;&#21046;&#30340;&#33021;&#21147;&#12290;&#20854;&#22797;&#26434;&#24230;&#21487;&#20197;&#26681;&#25454;&#39033;&#30446;&#30340;&#38656;&#27714;&#22312;&#19981;&#21516;&#32423;&#21035;&#19978;&#23454;&#29616;&#12290;&#20973;&#20511;&#22312;&#31867;&#22411;&#22788;&#29702;&#21644;&#36164;&#28304;&#31649;&#29702;&#31561;&#39046;&#22495;&#30340;&#28789;&#27963;&#24615;&#65292;&ldquo;&#22768;&#26126;&rdquo;&#20316;&#20026;&#24037;&#20855;&#65292;&#24403;&#25484;&#25569;&#24471;&#24403;&#26102;&#65292;&#21487;&#20197;&#26174;&#33879;&#22686;&#24378;PHP&#24212;&#29992;&#31243;&#24207;&#30340;&#20581;&#22766;&#24615;&#21644;&#21487;&#38752;&#24615;&#12290;<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#20171;&#32461; &#29702;&#35299;PHP&#20013;&#30340;&ldquo;declare&rdquo;&#26500;&#36896;&#21487;&#33021;&#19981;&#26159;&#27599;&#20010;PHP&#24320;&#21457;&#038;#32..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[38],"tuisongtax":[],"class_list":["post-426","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\/426","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=426"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=426"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=426"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}