{"id":615,"date":"2017-04-19T16:23:13","date_gmt":"2017-04-19T08:23:13","guid":{"rendered":"http:\/\/www.rainweb.site\/?p=615"},"modified":"2023-08-07T20:57:28","modified_gmt":"2023-08-07T12:57:28","slug":"php-article70","status":"publish","type":"post","link":"http:\/\/rain1024.com\/index.php\/2017\/04\/19\/php-article70\/","title":{"rendered":"PHP\u4e2dempty,isset,is_null\u7528\u6cd5\u548c\u533a\u522b"},"content":{"rendered":"<h3>PHP\u4e2dempty,isset,is_null\u7528\u6cd5\u548c\u533a\u522b<\/h3>\n<h4>\u6700\u8fd1\u5728\u9605\u8bfb\u9879\u76ee\u7684\u6e90\u7801\uff0c\u53d1\u73b0\u6e90\u7801\u4e2d\u5c31\u5bf9empty\u3001isset\u548cis_null\u51fd\u6570\uff08\u8bed\u8a00\u7279\u6027\uff09\u4e71\u7528\uff0c\u6709\u7684\u5730\u65b9\u5f88\u660e\u663e\u7684\u5c31\u6316\u5751\u4e86\u3002\u4e0d\u80fd\u6b63\u786e\u7684\u53bb\u7406\u89e3\u8fd9\u4e9b\u4e1c\u897f\uff0c\u5c31\u5f88\u53ef\u80fd\u7ed9\u540e\u7eed\u7684\u5f00\u53d1\u6316\u5751\u4e86\u3002<\/h4>\n<ol>\n<li>empty\u7528\u6cd5<br \/>\nbool empty ( mixed var)<br \/>\n\u5982\u679c var \u662f\u975e\u7a7a\u6216\u975e\u96f6\u7684\u503c\uff0c\u5219 empty() \u8fd4\u56de FALSE\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\"\"\u30010\u3001\"0\"\u3001NULL\u3001FALSE\u3001array()\u3001var $var; \u4ee5\u53ca\u6ca1\u6709\u4efb\u4f55\u5c5e\u6027\u7684\u5bf9\u8c61\u90fd\u5c06\u88ab\u8ba4\u4e3a\u662f\u7a7a\u7684\uff0c\u5982\u679c var \u4e3a\u7a7a\uff0c\u5219\u8fd4\u56de TRUE<\/p>\n<\/li>\n<li>\n<p>isset()<br \/>\nisset -- \u68c0\u6d4b\u53d8\u91cf\u662f\u5426\u8bbe\u7f6e<br \/>\n\u63cf\u8ff0<br \/>\nbool isset ( mixed var [, mixed var [, ...]])<br \/>\n\u5982\u679c var \u5b58\u5728\u5219\u8fd4\u56de TRUE\uff0c\u5426\u5219\u8fd4\u56de FALSE\u3002<br \/>\n\u5982\u679c\u5df2\u7ecf\u4f7f\u7528 unset() \u91ca\u653e\u4e86\u4e00\u4e2a\u53d8\u91cf\u4e4b\u540e\uff0c\u5b83\u5c06\u4e0d\u518d\u662f isset()\u3002\u82e5\u4f7f\u7528 isset() \u6d4b\u8bd5\u4e00\u4e2a\u88ab\u8bbe\u7f6e\u6210 NULL \u7684\u53d8\u91cf\uff0c\u5c06\u8fd4\u56de FALSE\u3002\u540c\u65f6\u8981\u6ce8\u610f\u7684\u662f\u4e00\u4e2a NULL \u5b57\u8282\uff08\"0\"\uff09\u5e76\u4e0d\u7b49\u540c\u4e8e PHP \u7684 NULL \u5e38\u6570\u3002<br \/>\n\u6ce8\u610f\uff1a\u5982\u679c\u53d8\u91cf\u4e0d\u5b58\u5728\uff0cisset()\u548cempty()\u90fd\u4e0d\u4f1a\u62a5\u9519\uff1bis_null(),is_numeric()\u4f1a\u62a5\u9519<br \/>\n\u5982\u4f55\u533a\u522b\u5982\u4e0b\u6570\u7ec4\u4e2d [0,'',null] \u4e09\u4e2a\u5143\u7d20<br \/>\n1.\u533a\u522b0:<\/p>\n<pre><code>$a = 0;\nisset($a) &amp;&amp; is_numeric($a) === true\n<\/code><\/pre>\n<p>2.\u533a\u522b''<\/p>\n<pre><code>$a = '';\nempty($a) &amp;&amp; $a=== ''\n<\/code><\/pre>\n<p>3.\u533a\u522bnull<\/p>\n<pre><code>$a = null;\nis_null($a);\n<\/code><\/pre>\n<p>\u53e6\u5916\u5728\u505a\u8868\u5355\u63d0\u4ea4\u7684\u65f6\u5019\u53ef\u80fd\u7ecf\u5e38\u8981\u68c0\u6d4b\u4e00\u4e2a\u53d8\u91cf\u662f\u5426\u5b58\u5728\uff0c\u5047\u5982<code>$_REQUEST['status'] = 0;<\/code>\u4f7f\u7528<code>empty($_REQUEST['status'])<\/code>\u8fd4\u56detrue\uff0c\u4f46\u662f\u4f7f\u7528<code>isset($_REQUEST['status'])<\/code> \u5224\u65ad\u5c31\u4e0d\u662f\u7a7a<\/p>\n<\/li>\n<li>\n<p>is_null()\uff1a<br \/>\n<code>bool is_null ( mixed $var )<\/code>  (php.net\u5b98\u65b9\u6587\u6863\u7684\u51fd\u6570\u5b9a\u4e49)<br \/>\n\u5f53\u53c2\u6570\u6ee1\u8db3\u4e0b\u9762\u4e09\u79cd\u60c5\u51b5\u65f6\uff0cis_null()\u5c06\u8fd4\u56deTRUE\uff0c\u5176\u5b83\u7684\u60c5\u51b5\u5c31\u662fFALSE<br \/>\n1\u3001\u5b83\u88ab\u8d4b\u503c\u4e3aNULL<br \/>\n2\u3001\u5b83\u8fd8\u6ca1\u6709\u8d4b\u503c<br \/>\n3\u3001\u5b83\u672a\u5b9a\u4e49\uff0c\u76f8\u5f53\u4e8eunset(),\u5c06\u4e00\u4e2a\u53d8\u91cfunset()\u540e\uff0c\u4e0d\u5c31\u662f\u6ca1\u6709\u5b9a\u4e49\u5417<br \/>\n\u8ba9\u6211\u4eec\u6765\u770b\u4e00\u4e9b\u4f8b\u5b50\uff1a<\/p>\n<\/li>\n<\/ol>\n<pre><code>$myvar = NULL;  \nvar_dump(is_null($myvar)); \/\/ TRUE \n$myvar1;      \nvar_dump(is_null($myvar1)); \/\/ TRUE Notice: Undefined variable \n$num = 520; \nunset($num); \nvar_dump(is_null($num)); \/\/TRUE Notice: Undefined variable \nvar_dump(is_null($some_undefined_var)); \/\/TRUE Notice: Undefined variable \n$myvar = 0; is_null($myvar);   \/\/ FALSE \n$myvar = FALSE; is_null($myvar); \/\/ FALSE \n$myvar = ''; is_null($myvar);  \/\/ FALSE\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u4e2dempty,isset,is_null\u7528\u6cd5\u548c\u533a\u522b \u6700\u8fd1\u5728\u9605\u8bfb\u9879\u76ee\u7684\u6e90\u7801\uff0c\u53d1\u73b0\u6e90\u7801\u4e2d\u5c31\u5bf9empty\u3001is\u2026 <span class=\"read-more\"><a href=\"http:\/\/rain1024.com\/index.php\/2017\/04\/19\/php-article70\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":575,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"footnotes":""},"categories":[34],"tags":[57],"class_list":["post-615","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-php","tag-php","post_format-post-format-aside"],"_links":{"self":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/615","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/comments?post=615"}],"version-history":[{"count":1,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/615\/revisions"}],"predecessor-version":[{"id":1467,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/posts\/615\/revisions\/1467"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media\/575"}],"wp:attachment":[{"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/media?parent=615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/categories?post=615"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/rain1024.com\/index.php\/wp-json\/wp\/v2\/tags?post=615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}