{"id":14,"date":"2014-07-02T18:24:51","date_gmt":"2014-07-02T18:24:51","guid":{"rendered":"https:\/\/jmrowe.com\/blog\/?p=14"},"modified":"2014-07-03T15:54:39","modified_gmt":"2014-07-03T15:54:39","slug":"proper-way-to-check-if-post-variable-is-actually-set-and-not-a-empty-string-or-null","status":"publish","type":"post","link":"https:\/\/jmrowe.com\/blog\/proper-way-to-check-if-post-variable-is-actually-set-and-not-a-empty-string-or-null\/","title":{"rendered":"Proper way to check if POST variable is actually set and not a empty string or NULL"},"content":{"rendered":"<p>Most people use isset to check if a POST variable \u00a0( i.e. isset(_POST(&#8216;textfield&#8217;)) \u00a0) has been entered. However, as the table displays below, if the user enters an empty string ( &#8220;&#8221;) isset will give an unexpected result.<\/p>\n<p>Instead we should use the following:<\/p>\n<pre class=\"lang:default decode:true \">array_key_exists('textfield', $_POST) &amp;&amp; !empty($_POST['textfield'])<\/pre>\n<p>&nbsp;<\/p>\n<p>The combination of array_key_exists and empty will check if the value is not only set but also not an empty string ( &#8220;&#8221;) or NULL.<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<th>Value of variable ($var)<\/th>\n<th>isset($var)<\/th>\n<th>empty($var)<\/th>\n<th>is_null($var)<\/th>\n<\/tr>\n<tr>\n<td>&#8220;&#8221; (an empty string)<\/td>\n<td>bool(true)<\/td>\n<td>bool(true)<\/td>\n<td>bool(false)<\/td>\n<\/tr>\n<tr>\n<td>&#8221; &#8221; (space)<\/td>\n<td>bool(true)<\/td>\n<td>bool(false)<\/td>\n<td>bool(false)<\/td>\n<\/tr>\n<tr>\n<td>FALSE<\/td>\n<td>bool(true)<\/td>\n<td>bool(true)<\/td>\n<td>bool(false)<\/td>\n<\/tr>\n<tr>\n<td>TRUE<\/td>\n<td>bool(true)<\/td>\n<td>bool(false)<\/td>\n<td>bool(false)<\/td>\n<\/tr>\n<tr>\n<td>array() (an empty array)<\/td>\n<td>bool(true)<\/td>\n<td>bool(true)<\/td>\n<td>bool(false)<\/td>\n<\/tr>\n<tr>\n<td>NULL<\/td>\n<td>bool(false)<\/td>\n<td>bool(true)<\/td>\n<td>bool(true)<\/td>\n<\/tr>\n<tr>\n<td>&#8220;0&#8221; (0 as a string)<\/td>\n<td>bool(true)<\/td>\n<td>bool(true)<\/td>\n<td>bool(false)<\/td>\n<\/tr>\n<tr>\n<td>0 (0 as an integer)<\/td>\n<td>bool(true)<\/td>\n<td>bool(true)<\/td>\n<td>bool(false)<\/td>\n<\/tr>\n<tr>\n<td>0.0 (0 as a float)<\/td>\n<td>bool(true)<\/td>\n<td>bool(true)<\/td>\n<td>bool(false)<\/td>\n<\/tr>\n<tr>\n<td>var $var; (a variable declared, but without a value)<\/td>\n<td>bool(false)<\/td>\n<td>bool(true)<\/td>\n<td>bool(true)<\/td>\n<\/tr>\n<tr>\n<td>NULL byte (&#8220;\\ 0&#8221;)<\/td>\n<td>bool(true)<\/td>\n<td>bool(false)<\/td>\n<td>bool(false)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div style=\"color: #000000;\">Table Source:\u00a0<a title=\"http:\/\/techtalk.virendrachandak.com\/php-isset-vs-empty-vs-is_null\/\" href=\"http:\/\/techtalk.virendrachandak.com\/php-isset-vs-empty-vs-is_null\/\" target=\"_blank\">http:\/\/techtalk.virendrachandak.com\/php-isset-vs-empty-vs-is_null\/<\/a><\/div>\n<p><span style=\"color: #000000;\">Table Credit to:\u00a0<a style=\"color: #28497c;\" href=\"http:\/\/ec.tynt.com\/b\/rw?id=cIw852sN4r44npacwqm_6r&amp;u=virendrachandak\" target=\"_blank\">@virendrachandak on Twitter<\/a><\/span><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most people use isset to check if a POST variable \u00a0( i.e. isset(_POST(&#8216;textfield&#8217;)) \u00a0) has been entered. However, as the table displays below, if the user enters an empty string ( &#8220;&#8221;) isset will give an unexpected result. Instead we should use the following: array_key_exists(&#8216;textfield&#8217;, $_POST) &amp;&amp; !empty($_POST[&#8216;textfield&#8217;]) &nbsp; The combination of array_key_exists and empty [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-14","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/jmrowe.com\/blog\/wp-json\/wp\/v2\/posts\/14","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jmrowe.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jmrowe.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jmrowe.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jmrowe.com\/blog\/wp-json\/wp\/v2\/comments?post=14"}],"version-history":[{"count":4,"href":"https:\/\/jmrowe.com\/blog\/wp-json\/wp\/v2\/posts\/14\/revisions"}],"predecessor-version":[{"id":22,"href":"https:\/\/jmrowe.com\/blog\/wp-json\/wp\/v2\/posts\/14\/revisions\/22"}],"wp:attachment":[{"href":"https:\/\/jmrowe.com\/blog\/wp-json\/wp\/v2\/media?parent=14"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jmrowe.com\/blog\/wp-json\/wp\/v2\/categories?post=14"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jmrowe.com\/blog\/wp-json\/wp\/v2\/tags?post=14"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}