{"id":483,"date":"2025-06-10T21:07:15","date_gmt":"2025-06-10T13:07:15","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/jcyy\/483.html"},"modified":"2025-06-10T21:07:15","modified_gmt":"2025-06-10T13:07:15","slug":"php%e4%b8%ad%e7%9a%84%e5%8f%af%e7%a9%ba%e7%b1%bb%e5%9e%8b%ef%bc%9a%e5%ae%9e%e7%94%a8%e6%8c%87%e5%8d%97%ef%bc%885%e4%b8%aa%e7%a4%ba%e4%be%8b%ef%bc%89","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/483.html","title":{"rendered":"PHP\u4e2d\u7684\u53ef\u7a7a\u7c7b\u578b\uff1a\u5b9e\u7528\u6307\u5357\uff085\u4e2a\u793a\u4f8b\uff09"},"content":{"rendered":"<p><body><\/p>\n<h2>&#27010;&#35272;<\/h2>\n<p>&#22312;&#19981;&#26029;&#28436;&#21464;&#30340;Web&#24320;&#21457;&#39046;&#22495;&#65292;PHP&#19968;&#30452;&#20316;&#20026;&#21518;&#31471;&#25216;&#26415;&#30340;&#22522;&#30784;&#23384;&#22312;&#12290;&#38543;&#30528;&#24212;&#29992;&#31243;&#24207;&#22797;&#26434;&#24615;&#30340;&#22686;&#21152;&#65292;&#22788;&#29702;&#20540;&#32570;&#22833;&#25110;&ldquo;null&rdquo;&#27010;&#24565;&#21464;&#24471;&#36234;&#26469;&#36234;&#37325;&#35201;&#12290;PHP 7.1&#24341;&#20837;&#20102;&#21487;&#36873;&#31867;&#22411;&#65292;&#20026;&#35299;&#20915;&#36825;&#19968;&#38382;&#39064;&#25552;&#20379;&#20102;&#20248;&#38597;&#30340;&#35299;&#20915;&#26041;&#26696;&#12290;&#26412;&#25991;&#36890;&#36807;&#20116;&#20010;&#31034;&#20363;&#65292;&#20174;&#22522;&#30784;&#21040;&#26356;&#39640;&#32423;&#21035;&#20351;&#29992;&#26696;&#20363;&#65292;&#25506;&#35752;&#20102;PHP&#20013;&#30340;&#21487;&#36873;&#31867;&#22411;&#30340;&#23454;&#38469;&#24212;&#29992;&#12290;<\/p>\n<h2>&#29702;&#35299;&#21487;&#36873;&#31867;&#22411;<\/h2>\n<p>&#22312;&#28145;&#20837;&#20363;&#23376;&#20043;&#21069;&#65292;&#35753;&#25105;&#20204;&#20808;&#20102;&#35299;&#19968;&#19979;&#20160;&#20040;&#26159;&#21487;&#36873;&#31867;&#22411;&#12290;&#22312;PHP&#20013;&#65292;&#21464;&#37327;&#21487;&#20197;&#26126;&#30830;&#22320;&#35774;&#32622;&#20026;&#21487;&#36873;&#31867;&#22411;&#12290;<code>null<\/code>&#20026;&#20102;&#34920;&#31034;&#19968;&#20010;&#20540;&#30340;&#32570;&#22833;&#65292;&#20294;&#26159;&#22312;PHP 7.1&#20043;&#21069;&#65292;&#22312;&#20989;&#25968;&#25110;&#26041;&#27861;&#20013;&#22768;&#26126;&#30340;&#31867;&#22411;&#19981;&#33021;&#25509;&#21463;<code>null<\/code>&#38500;&#38750;&#26126;&#30830;&#36825;&#26679;&#20570;&#12290;&#20351;&#29992;&#21487;&#36873;&#31867;&#22411;&#26102;&#65292;&#21487;&#20197;&#22312;&#31867;&#22411;&#22768;&#26126;&#21069;&#21152;&#19978;&#38382;&#21495;&#12290;<code>?<\/code>&#34920;&#31034;&#21442;&#25968;&#21487;&#20197;&#25351;&#23450;&#20026;&#26576;&#31181;&#31867;&#22411;&#65292;&#25110;&#32773;&#20063;&#21487;&#20197;&#26159;&#21478;&#19968;&#31181;&#31867;&#22411;&#30340;&#20540;&#12290;<code>null<\/code>&#24050;&#32463;&#25910;&#21040;&#65292;&#26377;&#20160;&#20040;&#25105;&#21487;&#20197;&#24110;&#21161;&#30340;&#21527;&#65311;<\/p>\n<h2>&#31034;&#20363; 1&#65306;&#22522;&#26412;&#21487;&#36873;&#31867;&#22411;&#22768;&#26126;<\/h2>\n<pre><code>function getUserName(?string $name): ?string {\n    return $name;\n}\n\n\/\/ Calling with a string\necho getUserName(\"John\"); \/\/ Outputs: John\n\n\/\/ Calling with null\necho getUserName(null); \/\/ Outputs nothing\n<\/code><\/pre>\n<p>&#36825;&#20010;&#31616;&#21333;&#30340;&#20989;&#25968;&#23637;&#31034;&#20102;&#21442;&#25968;&#21644;&#36820;&#22238;&#31867;&#22411;&#22914;&#20309;&#26126;&#30830;&#25509;&#21463;&#25351;&#23450;&#30340;&#31867;&#22411;&#12290;<code>null<\/code>&#22909;&#30340;&#65292;&#35831;&#21457;&#36865;&#20320;&#38656;&#35201;&#32763;&#35793;&#30340;&#20869;&#23481;&#12290;<\/p>\n<h2>&#31034;&#20363; 2&#65306;&#31867;&#23646;&#24615;&#20013;&#30340;&#21487;&#36873;&#31867;&#22411;<\/h2>\n<p>&#22312;PHP 7.4&#20013;&#65292;&#24341;&#20837;&#20102;&#23646;&#24615;&#31867;&#22411;&#22768;&#26126;&#65292;&#20351;&#24471;&#23646;&#24615;&#21487;&#20197;&#20026;null&#12290;&#19979;&#38754;&#26159;&#20351;&#29992;&#21487;&#36873;&#31867;&#22411;&#30340;&#31867;&#23646;&#24615;&#30340;&#26041;&#27861;&#65306;<\/p>\n<pre><code>class User {\n    public ?string $name = null;\n}\n\n$user = new User();\n$user-&gt;name = \"Alice\";\n\/\/ Or set to null\n$user-&gt;name = null;\n<\/code><\/pre>\n<p>&#36825;&#20351;&#24471;&#23545;&#31867;&#23646;&#24615;&#30340;&#31867;&#22411;&#22768;&#26126;&#26356;&#21152;&#31283;&#20581;&#65292;&#20174;&#32780;&#20419;&#36827;&#26356;&#25972;&#27905;&#21644;&#21487;&#39044;&#27979;&#30340;&#20195;&#30721;&#12290;<\/p>\n<h2>&#31034;&#20363; 3&#65306;&#32467;&#21512;&#21487;&#36873;&#31867;&#22411;&#24182;&#20351;&#29992;&#40664;&#35748; null &#20540;<\/h2>\n<p>&#21442;&#25968;&#22768;&#26126;&#21487;&#20197;&#19982;&#40664;&#35748;&#20540;&#19968;&#36215;&#20351;&#29992;&#12290;<code>null<\/code>&#20026;&#20102;&#36827;&#19968;&#27493;&#31616;&#21270;&#21487;&#36873;&#21442;&#25968;&#30340;&#22788;&#29702;&#65306;<\/p>\n<pre><code>function setLanguage(?string $lang = null) {\n    echo \"Language: \" . ($lang ?? \"Not specified\");\n}\n\nsetLanguage(\"PHP\"); \/\/ Outputs: Language: PHP\nsetLanguage(); \/\/ Outputs: Language: Not specified\n<\/code><\/pre>\n<p>&#36825;&#31181;&#27169;&#24335;&#38750;&#24120;&#36866;&#21512;&#37027;&#20123;&#21487;&#20197;&#19981;&#25509;&#21463;&#21442;&#25968;&#30340;&#20989;&#25968;&#65292;&#21516;&#26102;&#21448;&#33021;&#20445;&#35777;&#31867;&#22411;&#23433;&#20840;&#24615;&#12290;<\/p>\n<h2>&#31034;&#20363; 4&#65306;&#21487;&#36873;&#36820;&#22238;&#31867;&#22411;&#21644;&#38169;&#35823;&#22788;&#29702;<\/h2>\n<p>&#21487;&#36873;&#36820;&#22238;&#31867;&#22411;&#22312;&#38169;&#35823;&#22788;&#29702;&#26041;&#38754;&#23588;&#20854;&#26377;&#29992;&#65292;&#24403;&#19968;&#20010;&#25104;&#21151;&#30340;&#25805;&#20316;&#36820;&#22238;&#32467;&#26524;&#65292;&#32780;&#19968;&#20010;&#22833;&#36133;&#30340;&#25805;&#20316;&#21017;&#36820;&#22238;&#19968;&#20010;&#24322;&#24120;&#26102;&#65292;&#29305;&#21035;&#26377;&#25928;&#12290;<code>null<\/code>&#22909;&#30340;&#65292;&#35831;&#25552;&#20379;&#38656;&#35201;&#32763;&#35793;&#30340;&#20855;&#20307;&#20869;&#23481;&#25110;&#25351;&#20196;&#65292;&#25105;&#23558;&#36827;&#34892;&#32763;&#35793;&#12290;<\/p>\n<pre><code>function findProduct(int $id): ?array {\n    \/\/ Simulate searching for a product\n    $product = $id === 1 ? ['id' =&gt; 1, 'name' =&gt; 'Laptop'] : null;\n    return $product;\n}\n\n$product = findProduct(1);\nif ($product !== null) {\n    echo \"Product found: \" . $product['name'];\n} else {\n    echo \"Product not found.\";\n}\n<\/code><\/pre>\n<p>&#36890;&#36807;&#20351;&#29992;&#21487;&#36873;&#36820;&#22238;&#31867;&#22411;&#65292;&#20989;&#25968;&#21487;&#20197;&#26356;&#26126;&#30830;&#22320;&#34920;&#36798;&#39044;&#26399;&#30340;&#32467;&#26524;&#65292;&#20174;&#32780;&#25552;&#39640;&#20195;&#30721;&#30340;&#21487;&#35835;&#24615;&#21644;&#21487;&#38752;&#24615;&#12290;<\/p>\n<h2>&#31034;&#20363; 5&#65306;&#20351;&#29992;&#31867;&#22411;&#23646;&#24615;&#30340;&#39640;&#32423;&#29992;&#27861;<\/h2>\n<p>&#22522;&#20110;&#31034;&#20363;2&#65292;&#22312;PHP 7.4&#21450;&#26356;&#39640;&#29256;&#26412;&#20013;&#65292;&#20320;&#21487;&#20197;&#32467;&#21512;nullable&#31867;&#22411;&#19982;typed&#23646;&#24615;&#20197;&#23454;&#29616;&#26356;&#22797;&#26434;&#30340;&#22330;&#26223;&#65292;&#20363;&#22914;&#20381;&#36182;&#27880;&#20837;&#65306;<\/p>\n<pre><code>class DatabaseConnection {\n    public function __construct(public ?mysqli $connection = null) {}\n}\n\n\/\/ Create a new instance without a connection\n$dbConnection = new DatabaseConnection();\n\n\/\/ Later inject the mysqli connection\n$dbConnection-&gt;connection = new mysqli(\"localhost\", \"user\", \"pass\", \"database_name\");\n<\/code><\/pre>\n<p>&#22312;&#36825;&#31181;&#27169;&#24335;&#20013;&#65292;&#31867;&#23646;&#24615;&#21487;&#20197;&#26126;&#30830;&#34920;&#31034;&#20381;&#36182;&#39033;&#26159;&#21542;&#20026;&#21487;&#36873;&#30340;&#65292;&#20174;&#32780;&#20351;&#20195;&#30721;&#26356;&#21152;&#28789;&#27963;&#21644;&#35821;&#20041;&#28165;&#26224;&#12290;<\/p>\n<h2>&#32467;&#35770;<\/h2>\n<p>&#22312;PHP&#20013;&#20351;&#29992;&#21487;&#36873;&#31867;&#22411;&#22823;&#22823;&#22686;&#24378;&#20102;&#35821;&#35328;&#30340;&#31867;&#22411;&#31995;&#32479;&#65292;&#20351;&#20195;&#30721;&#26356;&#20855;&#34920;&#36798;&#21147;&#19988;&#20943;&#23569;&#38169;&#35823;&#12290;&#36890;&#36807;&#21033;&#29992;&#21487;&#36873;&#31867;&#22411;&#65292;&#24320;&#21457;&#20154;&#21592;&#21487;&#20197;&#32534;&#20889;&#26356;&#21487;&#38752;&#30340;&#12289;&#26131;&#20110;&#38405;&#35835;&#21644;&#31649;&#29702;&#30340;&#20195;&#30721;&#12290;&#38543;&#30528;PHP&#30340;&#21457;&#23637;&#65292;&#29702;&#35299;&#21644;&#23454;&#26045;&#36825;&#20123;&#26041;&#38754;&#23545;&#20110;&#29616;&#20195;PHP&#24320;&#21457;&#21464;&#24471;&#19981;&#21487;&#25110;&#32570;&#12290;<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#27010;&#35272; &#22312;&#19981;&#26029;&#28436;&#21464;&#30340;Web&#24320;&#21457;&#39046;&#22495;&#65292;PHP&#19968;&#30452;&#20316;&#20026;&#21518;&#31471;&#038;..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[38],"tuisongtax":[],"class_list":["post-483","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\/483","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=483"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=483"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=483"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}