{"id":3270,"date":"2023-06-07T17:38:09","date_gmt":"2023-06-07T15:38:09","guid":{"rendered":"https:\/\/davidperezgar.com\/?p=3270"},"modified":"2024-07-29T08:43:53","modified_gmt":"2024-07-29T06:43:53","slug":"best-practices-in-web-development-using-empty","status":"publish","type":"post","link":"https:\/\/davidperezgar.com\/en\/blog\/code\/best-practices-in-web-development-using-empty\/","title":{"rendered":"Best practices in web development: use of empty"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>empty()<\/code> function in PHP is used to check if a variable exists and if its value is considered empty. It&#8217;s a good practice to make your code not break when you receive variable values that you have to secure <\/p> <p>.\n\n\n\n<p class=\"wp-block-paragraph\">Return <code>true<\/code> if the <strong>variable does not exist<\/strong> or if its value is one of the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><ul>\n<li>&#8220;&#8221; (an empty string)<\/li>\n\n\n\n<li>0 (0 as an integer)<\/li>\n\n\n\n<li>0.0 (0 as a float)<\/li>\n\n\n\n<li>&#8220;0&#8221; (0 as a string)<\/li>\n\n\n\n<li>null<\/li>\n\n\n\n<li>false<\/li>\n\n\n\n<li>array() (an empty array)<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Since <code>empty<\/code> checks that the variable exists, it would save us this statement:<\/p>.\n\n\n\n<div class=\"cbc-code-wrapper\"><div class=\"cbc-code-bar\"><button class=\"cbc-copy-button\">Copy<\/button><\/div><pre class=\"wp-block-code\"><code class=\"\">if ( isset( $order['client_name'] ) &amp;&amp; '' !== $order['client_name'] )<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">To this one which is much more abbreviated:<\/p>\n\n\n\n<div class=\"cbc-code-wrapper\"><div class=\"cbc-code-bar\"><button class=\"cbc-copy-button\">Copy<\/button><\/div><pre class=\"wp-block-code\"><code class=\"\"><code class=\"\">if ( ! empty( $order['client_name'] )<\/code><\/pre><\/div>\n\n\n\n<div class=\"cbc-code-wrapper\"><div class=\"cbc-code-bar\"><button class=\"cbc-copy-button\">Copy<\/button><\/div><pre class=\"wp-block-code\"><code class=\"\">if ( isset( $order( $order['client_name'] ) &amp;&amp; ! empty( $order['client_name'] ) )<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">For an array we should also add the check if it's an array:<\/p>\n\n\n\n<div class=\"cbc-code-wrapper\"><div class=\"cbc-code-bar\"><button class=\"cbc-copy-button\">Copy<\/button><\/div><pre class=\"wp-block-code\"><code class=\"\">if ( ! empty( $order ) &amp;&amp; is_array( $order ) )<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">And you could also use it in a ternary:<\/p> <p>.\n\n\n\n<div class=\"cbc-code-wrapper\"><div class=\"cbc-code-bar\"><button class=\"cbc-copy-button\">Copy<\/button><\/div><pre class=\"wp-block-code\"><code class=\"\"><code class=\"\">$client_name = ! empty( $order['client_name'] ) ? $order['client_name'] : '';<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">But in these cases, it is customary to use isset:<\/p>\n\n\n\n<div class=\"cbc-code-wrapper\"><div class=\"cbc-code-bar\"><button class=\"cbc-copy-button\">Copy<\/button><\/div><pre class=\"wp-block-code\"><code class=\"\">$client_name = isset( $order['client_name'] ) ? $order['client_name'] : '';<\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>empty()<\/code> function is commonly used in control flow structures, such as <code>if<\/code> and <code>while<\/code>, to check if a variable has a valid value before performing an action or making a decision based on that value. It can also be used in web forms to check whether or not a field has been filled in before processing the data submitted by the user.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The empty() function in PHP is used to check if a variable exists and if its value is considered empty. It is a good practice to make your code not to break when you receive variable values that you have to secure It returns true if the variable does not exist or if its value is one of the following: As empty checks that the variable exists, it would save us this statement: To this one which is much more abbreviated: For an array we should also add the check if it is an array: And you could also use it in a ternary: But in these cases, [&hellip;].<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","inline_featured_image":false,"_ayudawp_aiss_exclude":false,"webmentions_disabled_pings":false,"webmentions_disabled":false,"editor_notices":[],"footnotes":""},"categories":[550],"tags":[],"class_list":["post-3270","post","type-post","status-publish","format-standard","hentry","category-code","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50"],"_links":{"self":[{"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/posts\/3270","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/comments?post=3270"}],"version-history":[{"count":0,"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/posts\/3270\/revisions"}],"wp:attachment":[{"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/media?parent=3270"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/categories?post=3270"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/davidperezgar.com\/en\/wp-json\/wp\/v2\/tags?post=3270"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}