{"id":12302,"date":"2023-02-05T12:19:47","date_gmt":"2023-02-05T10:19:47","guid":{"rendered":"https:\/\/techmoz.net\/?p=12302"},"modified":"2023-02-05T12:19:47","modified_gmt":"2023-02-05T10:19:47","slug":"avoiding-issues-with-php-in_array-by-using-strict-parameter","status":"publish","type":"post","link":"https:\/\/techmoz.net\/en\/avoiding-issues-with-php-in_array-by-using-strict-parameter\/","title":{"rendered":"Avoiding issues with PHP in_array by using $strict parameter"},"content":{"rendered":"<p>In PHP, the in_array() function is a built-in function that is used to check if a value exists in an array. The function takes two mandatory arguments: the value to search for and the array in which to search for it.<\/p>\n<p>For example, the following code checks if the value &#8220;apple&#8221; exists in the array $fruits:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\r\n$fruits = array(&quot;banana&quot;, &quot;orange&quot;, &quot;apple&quot;, &quot;strawberry&quot;);\r\nif (in_array(&quot;apple&quot;, $fruits)) {\r\necho &quot;The value exists in the array.&quot;;\r\n} else {\r\necho &quot;The value does not exist in the array.&quot;;\r\n}\r\n\r\n<\/pre>\n<p>The above code will output &#8220;The value exists in the array.&#8221;<\/p>\n<p>It also accept an optional third argument, <strong>$strict<\/strong>, which when set to true, the function will also check the types of the values. So if you are looking for a value that is 0 in an array that contains &#8220;0&#8221; as well, using strict check will return false while non-strict check will return true.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\r\n$arr = array(0, &quot;0&quot;);\r\nif (in_array(&quot;0&quot;, $arr, true)) {\r\necho &quot;found 0 as a string&quot;;\r\n}\r\nelseif (in_array(0, $arr, true)) {\r\necho &quot;found 0 as an integer&quot;;\r\n}\r\n\r\n<\/pre>\n<p>This will output &#8220;found 0 as an integer&#8221;.<\/p>\n<p>It is important to note that if the third argument, $strict, is not passed to the function, it defaults to false. This means that the function will perform a loose comparison (==) instead of a strict comparison (===). This can lead to unexpected results, especially when searching for values of different types. For example, the following code will return true when the $strict parameter is not passed, because &#8220;0&#8221; is loosely equal to 0:<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\r\n$arr = array(&quot;1&quot;, &quot;2&quot;, &quot;3&quot;, &quot;0&quot;);\r\nif (in_array(0, $arr)) {\r\necho &quot;0 is in the array&quot;;\r\n}\r\n\r\n<\/pre>\n<p>This will output &#8220;0 is in the array&#8221;.<\/p>\n<p>However, this can cause problems in certain situations and it&#8217;s recommended to always pass true as the third argument to ensure that the function performs a strict comparison and returns the expected results.<\/p>\n<p>In summary, in_array() function is a useful function in PHP to check if a value exists in an array. But it&#8217;s dangerous if not passing the $strict parameter to avoid the unexpected results and it&#8217;s recommended to always use the $strict parameter for the better results.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In PHP, the in_array() function is a built-in function that is used to check if a value exists in an array. The function takes two mandatory arguments: the value to search for and the array in which to search for it. For example, the following code checks if the value &#8220;apple&#8221; exists in the array<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_dcms_eufi_img":"","footnotes":"","_links_to":"","_links_to_target":""},"categories":[963,964,974,952,989,991,1001],"tags":[],"class_list":["post-12302","post","type-post","status-publish","format-standard","hentry","category-articles-2","category-cakephp-2-development-tools","category-development-tools","category-highlights-2","category-php-2","category-programming","category-wordpress"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Avoiding issues with PHP in_array by using $strict parameter - Techmoz<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techmoz.net\/en\/avoiding-issues-with-php-in_array-by-using-strict-parameter\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Avoiding issues with PHP in_array by using $strict parameter - Techmoz\" \/>\n<meta property=\"og:description\" content=\"In PHP, the in_array() function is a built-in function that is used to check if a value exists in an array. The function takes two mandatory arguments: the value to search for and the array in which to search for it. For example, the following code checks if the value &#8220;apple&#8221; exists in the array\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techmoz.net\/en\/avoiding-issues-with-php-in_array-by-using-strict-parameter\/\" \/>\n<meta property=\"og:site_name\" content=\"Techmoz\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/web.facebook.com\/hostmoz\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-05T10:19:47+00:00\" \/>\n<meta name=\"author\" content=\"Elisio Leonardo\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@hostmoz\" \/>\n<meta name=\"twitter:site\" content=\"@hostmoz\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Elisio Leonardo\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"NewsArticle\"],\"@id\":\"https:\/\/techmoz.net\/en\/avoiding-issues-with-php-in_array-by-using-strict-parameter\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/techmoz.net\/en\/avoiding-issues-with-php-in_array-by-using-strict-parameter\/\"},\"author\":{\"name\":\"Elisio Leonardo\",\"@id\":\"https:\/\/techmoz.net\/#\/schema\/person\/dcc2c00505441293925f0573bc996b09\"},\"headline\":\"Avoiding issues with PHP in_array by using $strict parameter\",\"datePublished\":\"2023-02-05T10:19:47+00:00\",\"dateModified\":\"2023-02-05T10:19:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/techmoz.net\/en\/avoiding-issues-with-php-in_array-by-using-strict-parameter\/\"},\"wordCount\":402,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/techmoz.net\/#organization\"},\"articleSection\":[\"Articles\",\"CakePHP\",\"Development Tools\",\"Highlights\",\"PHP\",\"Programming\",\"Wordpress\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/techmoz.net\/en\/avoiding-issues-with-php-in_array-by-using-strict-parameter\/#respond\"]}],\"copyrightYear\":\"2023\",\"copyrightHolder\":{\"@id\":\"https:\/\/techmoz.net\/#organization\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/techmoz.net\/en\/avoiding-issues-with-php-in_array-by-using-strict-parameter\/\",\"url\":\"https:\/\/techmoz.net\/en\/avoiding-issues-with-php-in_array-by-using-strict-parameter\/\",\"name\":\"Avoiding issues with PHP in_array by using $strict parameter - Techmoz\",\"isPartOf\":{\"@id\":\"https:\/\/techmoz.net\/#website\"},\"datePublished\":\"2023-02-05T10:19:47+00:00\",\"dateModified\":\"2023-02-05T10:19:47+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/techmoz.net\/en\/avoiding-issues-with-php-in_array-by-using-strict-parameter\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/techmoz.net\/en\/avoiding-issues-with-php-in_array-by-using-strict-parameter\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/techmoz.net\/en\/avoiding-issues-with-php-in_array-by-using-strict-parameter\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/techmoz.net\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Avoiding issues with PHP in_array by using $strict parameter\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/techmoz.net\/#website\",\"url\":\"https:\/\/techmoz.net\/\",\"name\":\"Techmoz\",\"description\":\"O maior Portal de Tecnologia em Mo\u00e7ambique\",\"publisher\":{\"@id\":\"https:\/\/techmoz.net\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/techmoz.net\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/techmoz.net\/#organization\",\"name\":\"Hostmoz,Lda\",\"url\":\"https:\/\/techmoz.net\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/techmoz.net\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/techmoz.test\/wp-content\/uploads\/2023\/01\/techmoz-logo.png?fit=385%2C90&ssl=1\",\"contentUrl\":\"https:\/\/techmoz.test\/wp-content\/uploads\/2023\/01\/techmoz-logo.png?fit=385%2C90&ssl=1\",\"width\":385,\"height\":90,\"caption\":\"Hostmoz,Lda\"},\"image\":{\"@id\":\"https:\/\/techmoz.net\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/web.facebook.com\/hostmoz\",\"https:\/\/x.com\/hostmoz\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/techmoz.net\/#\/schema\/person\/dcc2c00505441293925f0573bc996b09\",\"name\":\"Elisio Leonardo\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/techmoz.net\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/80182e0df20d39fd5938f02fa54113c599d04372d288dc811ea9d480830a94fd?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/80182e0df20d39fd5938f02fa54113c599d04372d288dc811ea9d480830a94fd?s=96&d=mm&r=g\",\"caption\":\"Elisio Leonardo\"},\"description\":\"Elisio Leonardo is an experienced Web Developer, Solutions Architect, Digital Marketing Expert, and content producer with a passion for technology, artificial intelligence, web development, and entertainment. With nearly 15 years of writing engaging content on technology and entertainment, particularly Comic Book Movies, Elisio has become a trusted source of information in the digital landscape.\",\"url\":\"https:\/\/techmoz.net\/en\/author\/backstageel\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Avoiding issues with PHP in_array by using $strict parameter - Techmoz","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techmoz.net\/en\/avoiding-issues-with-php-in_array-by-using-strict-parameter\/","og_locale":"en_US","og_type":"article","og_title":"Avoiding issues with PHP in_array by using $strict parameter - Techmoz","og_description":"In PHP, the in_array() function is a built-in function that is used to check if a value exists in an array. The function takes two mandatory arguments: the value to search for and the array in which to search for it. For example, the following code checks if the value &#8220;apple&#8221; exists in the array","og_url":"https:\/\/techmoz.net\/en\/avoiding-issues-with-php-in_array-by-using-strict-parameter\/","og_site_name":"Techmoz","article_publisher":"https:\/\/web.facebook.com\/hostmoz","article_published_time":"2023-02-05T10:19:47+00:00","author":"Elisio Leonardo","twitter_card":"summary_large_image","twitter_creator":"@hostmoz","twitter_site":"@hostmoz","twitter_misc":{"Written by":"Elisio Leonardo","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","NewsArticle"],"@id":"https:\/\/techmoz.net\/en\/avoiding-issues-with-php-in_array-by-using-strict-parameter\/#article","isPartOf":{"@id":"https:\/\/techmoz.net\/en\/avoiding-issues-with-php-in_array-by-using-strict-parameter\/"},"author":{"name":"Elisio Leonardo","@id":"https:\/\/techmoz.net\/#\/schema\/person\/dcc2c00505441293925f0573bc996b09"},"headline":"Avoiding issues with PHP in_array by using $strict parameter","datePublished":"2023-02-05T10:19:47+00:00","dateModified":"2023-02-05T10:19:47+00:00","mainEntityOfPage":{"@id":"https:\/\/techmoz.net\/en\/avoiding-issues-with-php-in_array-by-using-strict-parameter\/"},"wordCount":402,"commentCount":1,"publisher":{"@id":"https:\/\/techmoz.net\/#organization"},"articleSection":["Articles","CakePHP","Development Tools","Highlights","PHP","Programming","Wordpress"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techmoz.net\/en\/avoiding-issues-with-php-in_array-by-using-strict-parameter\/#respond"]}],"copyrightYear":"2023","copyrightHolder":{"@id":"https:\/\/techmoz.net\/#organization"}},{"@type":"WebPage","@id":"https:\/\/techmoz.net\/en\/avoiding-issues-with-php-in_array-by-using-strict-parameter\/","url":"https:\/\/techmoz.net\/en\/avoiding-issues-with-php-in_array-by-using-strict-parameter\/","name":"Avoiding issues with PHP in_array by using $strict parameter - Techmoz","isPartOf":{"@id":"https:\/\/techmoz.net\/#website"},"datePublished":"2023-02-05T10:19:47+00:00","dateModified":"2023-02-05T10:19:47+00:00","breadcrumb":{"@id":"https:\/\/techmoz.net\/en\/avoiding-issues-with-php-in_array-by-using-strict-parameter\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techmoz.net\/en\/avoiding-issues-with-php-in_array-by-using-strict-parameter\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/techmoz.net\/en\/avoiding-issues-with-php-in_array-by-using-strict-parameter\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techmoz.net\/en\/"},{"@type":"ListItem","position":2,"name":"Avoiding issues with PHP in_array by using $strict parameter"}]},{"@type":"WebSite","@id":"https:\/\/techmoz.net\/#website","url":"https:\/\/techmoz.net\/","name":"Techmoz","description":"O maior Portal de Tecnologia em Mo\u00e7ambique","publisher":{"@id":"https:\/\/techmoz.net\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techmoz.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techmoz.net\/#organization","name":"Hostmoz,Lda","url":"https:\/\/techmoz.net\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techmoz.net\/#\/schema\/logo\/image\/","url":"https:\/\/techmoz.test\/wp-content\/uploads\/2023\/01\/techmoz-logo.png?fit=385%2C90&ssl=1","contentUrl":"https:\/\/techmoz.test\/wp-content\/uploads\/2023\/01\/techmoz-logo.png?fit=385%2C90&ssl=1","width":385,"height":90,"caption":"Hostmoz,Lda"},"image":{"@id":"https:\/\/techmoz.net\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/web.facebook.com\/hostmoz","https:\/\/x.com\/hostmoz"]},{"@type":"Person","@id":"https:\/\/techmoz.net\/#\/schema\/person\/dcc2c00505441293925f0573bc996b09","name":"Elisio Leonardo","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techmoz.net\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/80182e0df20d39fd5938f02fa54113c599d04372d288dc811ea9d480830a94fd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/80182e0df20d39fd5938f02fa54113c599d04372d288dc811ea9d480830a94fd?s=96&d=mm&r=g","caption":"Elisio Leonardo"},"description":"Elisio Leonardo is an experienced Web Developer, Solutions Architect, Digital Marketing Expert, and content producer with a passion for technology, artificial intelligence, web development, and entertainment. With nearly 15 years of writing engaging content on technology and entertainment, particularly Comic Book Movies, Elisio has become a trusted source of information in the digital landscape.","url":"https:\/\/techmoz.net\/en\/author\/backstageel\/"}]}},"_format-video":{"_last_editor_used_jetpack":["classic-editor"],"_edit_lock":["1675716018:2"],"_edit_last":["2"],"_wpml_media_featured":["1"],"_wpml_media_duplicate":["1"],"_syntaxhighlighter_encoded":["1"],"_encloseme":["1"],"views":["285"],"post_views_count":["1322"],"wp_review_type":["none"],"_post_template_options":["a:1:{s:17:\"single_post_style\";s:0:\"\";}"],"_format_video":["a:1:{s:13:\"embedded_link\";s:0:\"\";}"],"_wpb_vc_js_status":["false"],"_yoast_wpseo_newssitemap-exclude":["off"],"_yoast_wpseo_newssitemap-genre":["a:0:{}"],"_yoast_wpseo_content_score":["30"],"_yoast_wpseo_focuskeywords":["[]"],"_yoast_wpseo_keywordsynonyms":["[\"\"]"],"_yoast_wpseo_estimated-reading-time-minutes":["2"],"_yoast_wpseo_primary_category":["963"]},"_links":{"self":[{"href":"https:\/\/techmoz.net\/en\/wp-json\/wp\/v2\/posts\/12302","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techmoz.net\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techmoz.net\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techmoz.net\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/techmoz.net\/en\/wp-json\/wp\/v2\/comments?post=12302"}],"version-history":[{"count":2,"href":"https:\/\/techmoz.net\/en\/wp-json\/wp\/v2\/posts\/12302\/revisions"}],"predecessor-version":[{"id":12612,"href":"https:\/\/techmoz.net\/en\/wp-json\/wp\/v2\/posts\/12302\/revisions\/12612"}],"wp:attachment":[{"href":"https:\/\/techmoz.net\/en\/wp-json\/wp\/v2\/media?parent=12302"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techmoz.net\/en\/wp-json\/wp\/v2\/categories?post=12302"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techmoz.net\/en\/wp-json\/wp\/v2\/tags?post=12302"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}