{"id":48898,"date":"2024-12-02T08:54:38","date_gmt":"2024-12-02T00:54:38","guid":{"rendered":"https:\/\/fwq.ai\/blog\/48898\/"},"modified":"2024-12-02T08:54:38","modified_gmt":"2024-12-02T00:54:38","slug":"%e4%ba%86%e8%a7%a3-php-%e7%b1%bb%e5%9e%8b-null","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/48898\/","title":{"rendered":"\u4e86\u89e3 PHP \u7c7b\u578b &#8211; NULL"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u4e86\u89e3 PHP \u7c7b\u578b &#8211; NULL<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300a\u4e86\u89e3 PHP \u7c7b\u578b &#8211; NULL\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u7c73\u4e91\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241103\/17305938396726c42f3ce3b.jpg\" class=\"aligncenter\" title=\"\u4e86\u89e3 PHP \u7c7b\u578b &#8211; NULL\u63d2\u56fe\" alt=\"\u4e86\u89e3 PHP \u7c7b\u578b &#8211; NULL\u63d2\u56fe\" \/><\/p>\n<h2> \u4ecb\u7ecd <\/h2>\n<p>php \u4e2d\u7684 null \u7c7b\u578b\u8868\u793a\u6ca1\u6709\u4e3a\u5176\u8d4b\u503c\u7684\u53d8\u91cf\u3002\u5b83\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u6570\u636e\u7c7b\u578b\uff0c\u7528\u4e8e\u6307\u793a\u503c\u4e0d\u5b58\u5728\u6216\u663e\u5f0f\u58f0\u660e\u53d8\u91cf\u4e3a\u7a7a\u3002\u4e86\u89e3 null \u7684\u5de5\u4f5c\u539f\u7406\u4ee5\u53ca\u5982\u4f55\u6709\u6548\u5730\u4f7f\u7528\u5b83\u5bf9\u4e8e\u5728 php \u4e2d\u7ba1\u7406\u53d8\u91cf\u548c\u5904\u7406\u6761\u4ef6\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<hr>\n<h2> php \u4e2d\u4ec0\u4e48\u662f null\uff1f <\/h2>\n<p>\u5728 php \u4e2d\uff0c\u5982\u679c\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\uff0c\u53d8\u91cf\u5c06\u88ab\u89c6\u4e3a null\uff1a<\/p>\n<ul>\n<li>\u5b83\u5df2\u88ab\u5206\u914d\u5e38\u91cf null\u3002<\/li>\n<li>\u5c1a\u672a\u5206\u914d\u4efb\u4f55\u503c\u3002<\/li>\n<li>\u5df2\u4f7f\u7528 unset() \u51fd\u6570\u53d6\u6d88\u8bbe\u7f6e\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>&lt;?php\n$var1 = null;  \/\/ assigned null value explicitly\n$var2;         \/\/ no value assigned\nunset($var3);  \/\/ variable has been unset\n\nvar_dump($var1); \/\/ output: null\nvar_dump($var2); \/\/ output: null\nvar_dump($var3); \/\/ output: null\n?&gt;\n<\/pre>\n<hr>\n<h2> null \u7c7b\u578b\u7684\u7279\u5f81 <\/h2>\n<ul>\n<li> null \u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff0c\u56e0\u6b64 null\u3001null \u548c null \u90fd\u88ab\u89c6\u4e3a\u76f8\u540c\u3002<\/li>\n<li>\u5b83\u662f null \u7c7b\u578b\u7684\u552f\u4e00\u53ef\u80fd\u503c\u3002<\/li>\n<li>is_null() \u51fd\u6570\u53ef\u7528\u4e8e\u68c0\u67e5\u53d8\u91cf\u662f\u5426\u4e3a null\u3002<\/li>\n<\/ul>\n<hr>\n<h2> \u68c0\u67e5\u7a7a\u503c <\/h2>\n<p>\u8981\u68c0\u67e5\u53d8\u91cf\u662f\u5426\u4e3a null\uff0c\u53ef\u4ee5\u4f7f\u7528 is_null() \u51fd\u6570\u6216\u4e0e === \u8fdb\u884c\u4e25\u683c\u6bd4\u8f83\u3002<\/p>\n<p><strong>\u4f7f\u7528 is_null() \u7684\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>&lt;?php\n$var = null;\n\nif (is_null($var)) {\n    echo \"the variable is null.\";\n}\n?&gt;\n<\/pre>\n<p><strong>\u4f7f\u7528\u4e25\u683c\u6bd4\u8f83\u7684\u793a\u4f8b (===)\uff1a<\/strong><\/p>\n<pre>&lt;?php\n$var = null;\n\nif ($var === null) {\n    echo \"the variable is null.\";\n}\n?&gt;\n<\/pre>\n<hr>\n<h2> \u9ed8\u8ba4\u7a7a\u503c <\/h2>\n<p>\u5982\u679c\u58f0\u660e\u4e86\u53d8\u91cf\u4f46\u672a\u4e3a\u5176\u8d4b\u503c\uff0cphp \u4f1a\u81ea\u52a8\u4e3a\u5176\u5206\u914d null \u7c7b\u578b\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>&lt;?php\n$var;  \/\/ declared but not assigned a value\nvar_dump($var);  \/\/ output: null\n?&gt;\n<\/pre>\n<hr>\n<h2> \u53d6\u6d88\u8bbe\u7f6e\u53d8\u91cf <\/h2>\n<p>unset() \u51fd\u6570\u7528\u4e8e\u9500\u6bc1\u53d8\u91cf\u3002\u8c03\u7528unset()\u540e\uff0c\u53d8\u91cf\u53d8\u4e3anull\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>&lt;?php\n$var = 10;\nunset($var);\n\nvar_dump($var);  \/\/ output: null\n?&gt;\n<\/pre>\n<hr>\n<h2> \u5728\u6761\u4ef6\u8bed\u53e5\u4e2d\u4f7f\u7528 null <\/h2>\n<p>null \u5728\u5e03\u5c14\u4e0a\u4e0b\u6587\u4e2d\u88ab\u89c6\u4e3a false\u3002\u8fd9\u610f\u5473\u7740\u60a8\u53ef\u4ee5\u5728 if \u8bed\u53e5\u4e2d\u4f7f\u7528 null \u6765\u68c0\u67e5\u53d8\u91cf\u662f\u5426\u4e3a\u7a7a\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>&lt;?php\n$var = null;\n\nif (!$var) {\n    echo \"the variable is null or false.\";\n}\n?&gt;\n<\/pre>\n<hr>\n<h2> \u4f7f\u7528 null \u5408\u5e76\u8fd0\u7b97\u7b26 (??) \u8bbe\u7f6e\u9ed8\u8ba4\u503c <\/h2>\n<p><strong>php 7<\/strong> \u5f15\u5165\u4e86 null \u5408\u5e76\u8fd0\u7b97\u7b26 (??)\uff0c\u5b83\u5141\u8bb8\u60a8\u68c0\u67e5\u53d8\u91cf\u662f\u5426\u4e3a null\uff0c\u5982\u679c\u662f\u5219\u63d0\u4f9b\u9ed8\u8ba4\u503c\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>&lt;?php\n$username = null;\n\n\/\/ if $username is null, use \"guest\" as the default value\necho $username ?? \"guest\";  \/\/ output: guest\n?&gt;\n<\/pre>\n<p>\u8fd9\u5728\u5904\u7406\u53ef\u80fd\u5e76\u4e0d\u603b\u662f\u8bbe\u7f6e\u7684\u7528\u6237\u8f93\u5165\u6216\u914d\u7f6e\u65f6\u7279\u522b\u6709\u7528\u3002<\/p>\n<hr>\n<h2> php \u4e2d null \u7684\u5e38\u89c1\u7528\u4f8b <\/h2>\n<hr>\n<h3> \u521d\u59cb\u5316\u53d8\u91cf <\/h3>\n<p>\u5f53\u60a8\u8fd8\u6ca1\u6709\u53d8\u91cf\u503c\u65f6\uff0c\u4f7f\u7528 null \u663e\u5f0f\u521d\u59cb\u5316\u53d8\u91cf\u3002<\/p>\n<pre>$response = null;  \/\/ will be assigned a value later\n<\/pre>\n<hr>\n<h3> \u91cd\u7f6e\u503c <\/h3>\n<p>\u4f7f\u7528 null \u91cd\u7f6e\u53d8\u91cf\u7684\u503c\uff0c\u8868\u660e\u5b83\u4e0d\u518d\u4fdd\u5b58\u4efb\u4f55\u6570\u636e\u3002<\/p>\n<pre>$user_data = getuserdata();\n$user_data = null;  \/\/ free up memory\n<\/pre>\n<hr>\n<h3> \u51fd\u6570\u4e2d\u7684\u53ef\u9009\u53c2\u6570 <\/h3>\n<p>\u4f7f\u7528 null \u4f5c\u4e3a\u51fd\u6570\u4e2d\u53ef\u9009\u53c2\u6570\u7684\u9ed8\u8ba4\u503c\u3002<\/p>\n<pre>function greet($name = null) {\n    if ($name === null) {\n        echo \"Hello, Guest!\";\n    } else {\n        echo \"Hello, $name!\";\n    }\n}\n\ngreet();         \/\/ Output: Hello, Guest!\ngreet(\"John\");   \/\/ Output: Hello, John!\n<\/pre>\n<hr>\n<h2> \u7ed3\u8bba <\/h2>\n<p>php \u4e2d\u7684 null \u7c7b\u578b\u662f\u4e00\u4e2a\u57fa\u672c\u6982\u5ff5\uff0c\u8868\u793a\u503c\u4e0d\u5b58\u5728\u3002\u5b83\u7528\u9014\u5e7f\u6cdb\uff0c\u53ef\u7528\u4e8e\u521d\u59cb\u5316\u53d8\u91cf\u3001\u68c0\u67e5\u7a7a\u72b6\u6001\u4ee5\u53ca\u5904\u7406\u53ef\u9009\u53c2\u6570\u3002\u4e86\u89e3\u5982\u4f55\u4f7f\u7528 null \u5c06\u5e2e\u52a9\u60a8\u7f16\u5199\u66f4\u5065\u58ee\u4e14\u9632\u9519\u7684\u4ee3\u7801\u3002\u65e0\u8bba\u60a8\u662f\u68c0\u67e5\u672a\u8bbe\u7f6e\u7684\u53d8\u91cf\u8fd8\u662f\u4f7f\u7528 null \u5408\u5e76\u8fd0\u7b97\u7b26\uff0c\u638c\u63e1 null \u7c7b\u578b\u90fd\u662f\u7cbe\u901a php \u5f00\u53d1\u7684\u91cd\u8981\u7ec4\u6210\u90e8\u5206\u3002<\/p>\n<p>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300a\u4e86\u89e3 PHP \u7c7b\u578b &#8211; NULL\u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n<p> \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1adev.to \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3 PHP \u7c7b\u578b &#8211; NULL \u6536\u85cf \u7f16\u7a0b\u5e76\u4e0d\u662f\u4e00\u4e2a\u673a\u68b0\u6027\u7684\u5de5\u4f5c\uff0c\u800c\u662f\u9700\u8981\u6709\u601d\u8003\uff0c\u6709\u521b\u65b0\u7684\u5de5\u4f5c\uff0c\u8bed\u6cd5\u662f\u56fa\u5b9a\u7684\uff0c\u4f46\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u5219\u662f\u4f9d\u9760\u4eba\u7684\u601d\u7ef4\uff0c\u8fd9\u5c31\u9700\u8981\u6211\u4eec\u575a\u6301\u5b66\u4e60\u548c\u66f4\u65b0\u81ea\u5df1\u7684\u77e5\u8bc6\u3002\u4eca\u5929\u7c73\u4e91\u5c31\u6574\u7406\u5206\u4eab\u300a\u4e86\u89e3 PHP \u7c7b\u578b &#8211; NULL\u300b\uff0c\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec\uff0c\u5982\u679c\u4f60\u5bf9\u6587\u7ae0\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u5c31\u4e0d\u8981\u9519\u8fc7\u7c73\u4e91\uff0c\u5728\u8fd9\u53ef\u4ee5\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5f00\u53d1\u80fd\u529b\u7684\u63d0\u5347\u3002 \u4ecb\u7ecd php \u4e2d\u7684 null \u7c7b\u578b\u8868\u793a\u6ca1\u6709\u4e3a\u5176\u8d4b\u503c\u7684\u53d8\u91cf\u3002\u5b83\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u6570\u636e\u7c7b\u578b\uff0c\u7528\u4e8e\u6307\u793a\u503c\u4e0d\u5b58\u5728\u6216\u663e\u5f0f\u58f0\u660e\u53d8\u91cf\u4e3a\u7a7a\u3002\u4e86\u89e3 null \u7684\u5de5\u4f5c\u539f\u7406\u4ee5\u53ca\u5982\u4f55\u6709\u6548\u5730\u4f7f\u7528\u5b83\u5bf9\u4e8e\u5728 php \u4e2d\u7ba1\u7406\u53d8\u91cf\u548c\u5904\u7406\u6761\u4ef6\u81f3\u5173\u91cd\u8981\u3002 php \u4e2d\u4ec0\u4e48\u662f null\uff1f \u5728 php \u4e2d\uff0c\u5982\u679c\u6ee1\u8db3\u4ee5\u4e0b\u6761\u4ef6\uff0c\u53d8\u91cf\u5c06\u88ab\u89c6\u4e3a null\uff1a \u5b83\u5df2\u88ab\u5206\u914d\u5e38\u91cf null\u3002 \u5c1a\u672a\u5206\u914d\u4efb\u4f55\u503c\u3002 \u5df2\u4f7f\u7528 unset() \u51fd\u6570\u53d6\u6d88\u8bbe\u7f6e\u3002 \u793a\u4f8b\uff1a &lt;?php $var1 = null; \/\/ assigned null value explicitly $var2; \/\/ no value assigned unset($var3); \/\/ variable has been unset var_dump($var1); [&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-48898","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48898","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=48898"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/48898\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=48898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=48898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=48898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}