{"id":211,"date":"2018-08-07T22:30:24","date_gmt":"2018-08-07T22:30:24","guid":{"rendered":"http:\/\/www.projectimmerse.com\/?p=211"},"modified":"2020-06-17T02:36:37","modified_gmt":"2020-06-17T02:36:37","slug":"phps-is_numeric-function","status":"publish","type":"post","link":"https:\/\/www.projectimmerse.com\/blog\/phps-is_numeric-function\/","title":{"rendered":"PHP&#8217;s is_numeric function"},"content":{"rendered":"<p>I&#8217;ve been wanting to write about PHP&#8217;s <a href=\"http:\/\/php.net\/manual\/en\/function.is-numeric.php\" title=\"PHP is_numeric function\">is_numeric()<\/a> built-in function partly because I forgot all about it one day while validating one of my forms. <\/p>\n<p>I get super anal about form validation because if it&#8217;s anything that&#8217;ll make or break your web application it&#8217;s security. Something dirty gets through your form it&#8217;s game over &#8211; or just bad data, bad quality. This means bad user experience &#8211; see how this kind of thing can trickle down through your application you worked so hard on?<\/p>\n<p><!--more--><\/p>\n<p>So when I think validating against numbers in PHP, I think of data types. I remember my first programming jobs in the industry was a super low position at a start up company back in 2005. Don&#8217;t let the job position fool you though, this job as &#8220;web coder&#8221; as they called it those days (cause they couldn&#8217;t think of what to call it) was filled with golden nuggets &#8211; I would carry this toolset around wherever I would go and it has served me well. Anyway, I overheard the CTO of the company say one important phrase that seemed to stick with me to this day &#8211;<\/p>\n<blockquote><p>\nKnow your data types.\n<\/p><\/blockquote>\n<p>Yeah. That&#8217;s it. Ofcourse, I didn&#8217;t really know what that meant without experience &#8211; without breaking code or getting heat from managers for a projects delay. I learned over time how essential and fundamental knowing your data types were in programming, in any language. Data types are kind of squeezed in the stack between your development environment and the actual logic you write, so you have to keep this entire model in your head. And you know how easy it is for data types to slip through the cracks. <\/p>\n<p>So when I think of is_numeric() I think of <a href=\"http:\/\/php.net\/manual\/en\/function.is-int.php\" title=\"PHP is_int() function\">is_int<\/a>, <a href=\"http:\/\/php.net\/manual\/en\/function.is-null.php\" title=\"PHP is_null() function\">is_null()<\/a>, <a href=\"http:\/\/php.net\/manual\/en\/function.empty.php\" title=\"PHP empty() function\">empty()<\/a>, <a href=\"http:\/\/php.net\/manual\/en\/function.is-null.php\" title=\"PHP isset() function\">isset()<\/a> &#8211; other built in functions brought to you by PHP. The thing with each of them though is that they kind of overlap and at the same time you have to think about what is considered empty in PHP. For instance, &#8220;0&#8221; is considered false and empty in PHP. But &#8220;0&#8221; is a number so how do you check for that? <\/p>\n<p>Well is_numeric came to the rescue during one of my late nights of endless programming.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\r\n&lt;?php\r\n\r\n$number = 0;\r\n\r\nif (is_numeric($number)) {\r\n\r\n\techo 'is number';\r\n\t\r\n} else {\r\n\t\r\n\techo 'not a number';\r\n\t\r\n}\r\n\r\n<\/pre>\n<p>The above yields &#8220;is number&#8221; &#8211; that&#8217;s pretty sweet I think. <\/p>\n<p>But something to keep in mind is that the below will yield &#8220;not a number&#8221; &#8211; yeah, but see it is a number. &#8220;0&#8221; is a number. <\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\r\n$number = 0;\r\n\r\nif (!empty($number)) {\r\n\r\n\techo 'is number';\r\n\t\r\n} else {\r\n\t\r\n\techo 'not a number';\r\n\t\r\n}\r\n\r\n<\/pre>\n<p>So expanding this further given a scenario where you want to check if a field is empty and also make sure it&#8217;s a number and 0 is permitted, I came up with the following.<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n\r\n$number = 0;\r\n\r\nif (empty($number) &amp;&amp; !is_numeric($number)) {\r\n\r\n\techo 'not a number';\r\n\t\r\n} else {\r\n\t\r\n\tif(!is_numeric($number)) {\r\n\t\r\n\t\techo 'this field only accept numbers';\r\n\t\t\r\n\t} else {\r\n\t\t\r\n\t\r\n\t\techo 'passes test';\r\n\t}\r\n\t\r\n}\r\n\r\n<\/pre>\n<p>The above code will output &#8220;passes test&#8221; which is what we want. We want to capture that &#8220;0&#8221; as a number value. Now whether you want to type cast it as an int for good measure that&#8217;s up to you. <\/p>\n<p>It gets even tricker when you want to allow for whole positive numbers and decimal numbers for a given field. How would that play out? Then you&#8217;ll have to store this in the database &#8211; it gets pretty hairy. I&#8217;ll cover this on another, stay tuned. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been wanting to write about PHP&#8217;s is_numeric() built-in function partly because I forgot all about it one day while validating one of my forms. I get super anal about form validation because if it&#8217;s anything that&#8217;ll make or break your web application it&#8217;s security. Something dirty gets through your form it&#8217;s game over &#8211; &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.projectimmerse.com\/blog\/phps-is_numeric-function\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;PHP&#8217;s is_numeric function&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1133,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-211","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uncategorized"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PHP&#039;s is_numeric function - Project Immerse<\/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:\/\/www.projectimmerse.com\/blog\/phps-is_numeric-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PHP&#039;s is_numeric function - Project Immerse\" \/>\n<meta property=\"og:description\" content=\"I&#8217;ve been wanting to write about PHP&#8217;s is_numeric() built-in function partly because I forgot all about it one day while validating one of my forms. I get super anal about form validation because if it&#8217;s anything that&#8217;ll make or break your web application it&#8217;s security. Something dirty gets through your form it&#8217;s game over &#8211; &hellip; Continue reading &quot;PHP&#8217;s is_numeric function&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.projectimmerse.com\/blog\/phps-is_numeric-function\/\" \/>\n<meta property=\"og:site_name\" content=\"Project Immerse\" \/>\n<meta property=\"article:published_time\" content=\"2018-08-07T22:30:24+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-06-17T02:36:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2018\/08\/php-is-numeric-function.png\" \/>\n\t<meta property=\"og:image:width\" content=\"750\" \/>\n\t<meta property=\"og:image:height\" content=\"750\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"projectimmerse\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"projectimmerse\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/phps-is_numeric-function\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/phps-is_numeric-function\\\/\"},\"author\":{\"name\":\"projectimmerse\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#\\\/schema\\\/person\\\/c53f2864be524ee6fa08a7e4800dd1e5\"},\"headline\":\"PHP&#8217;s is_numeric function\",\"datePublished\":\"2018-08-07T22:30:24+00:00\",\"dateModified\":\"2020-06-17T02:36:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/phps-is_numeric-function\\\/\"},\"wordCount\":612,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/phps-is_numeric-function\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/08\\\/php-is-numeric-function.png\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/phps-is_numeric-function\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/phps-is_numeric-function\\\/\",\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/phps-is_numeric-function\\\/\",\"name\":\"PHP's is_numeric function - Project Immerse\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/phps-is_numeric-function\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/phps-is_numeric-function\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/08\\\/php-is-numeric-function.png\",\"datePublished\":\"2018-08-07T22:30:24+00:00\",\"dateModified\":\"2020-06-17T02:36:37+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#\\\/schema\\\/person\\\/c53f2864be524ee6fa08a7e4800dd1e5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/phps-is_numeric-function\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/phps-is_numeric-function\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/phps-is_numeric-function\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/08\\\/php-is-numeric-function.png\",\"contentUrl\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/08\\\/php-is-numeric-function.png\",\"width\":750,\"height\":750,\"caption\":\"PHP is_numeric function\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/phps-is_numeric-function\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PHP&#8217;s is_numeric function\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/\",\"name\":\"Project Immerse\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/#\\\/schema\\\/person\\\/c53f2864be524ee6fa08a7e4800dd1e5\",\"name\":\"projectimmerse\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g\",\"caption\":\"projectimmerse\"},\"url\":\"https:\\\/\\\/www.projectimmerse.com\\\/blog\\\/author\\\/projectimmerse\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PHP's is_numeric function - Project Immerse","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:\/\/www.projectimmerse.com\/blog\/phps-is_numeric-function\/","og_locale":"en_US","og_type":"article","og_title":"PHP's is_numeric function - Project Immerse","og_description":"I&#8217;ve been wanting to write about PHP&#8217;s is_numeric() built-in function partly because I forgot all about it one day while validating one of my forms. I get super anal about form validation because if it&#8217;s anything that&#8217;ll make or break your web application it&#8217;s security. Something dirty gets through your form it&#8217;s game over &#8211; &hellip; Continue reading \"PHP&#8217;s is_numeric function\"","og_url":"https:\/\/www.projectimmerse.com\/blog\/phps-is_numeric-function\/","og_site_name":"Project Immerse","article_published_time":"2018-08-07T22:30:24+00:00","article_modified_time":"2020-06-17T02:36:37+00:00","og_image":[{"width":750,"height":750,"url":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2018\/08\/php-is-numeric-function.png","type":"image\/png"}],"author":"projectimmerse","twitter_card":"summary_large_image","twitter_misc":{"Written by":"projectimmerse","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.projectimmerse.com\/blog\/phps-is_numeric-function\/#article","isPartOf":{"@id":"https:\/\/www.projectimmerse.com\/blog\/phps-is_numeric-function\/"},"author":{"name":"projectimmerse","@id":"https:\/\/www.projectimmerse.com\/blog\/#\/schema\/person\/c53f2864be524ee6fa08a7e4800dd1e5"},"headline":"PHP&#8217;s is_numeric function","datePublished":"2018-08-07T22:30:24+00:00","dateModified":"2020-06-17T02:36:37+00:00","mainEntityOfPage":{"@id":"https:\/\/www.projectimmerse.com\/blog\/phps-is_numeric-function\/"},"wordCount":612,"commentCount":0,"image":{"@id":"https:\/\/www.projectimmerse.com\/blog\/phps-is_numeric-function\/#primaryimage"},"thumbnailUrl":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2018\/08\/php-is-numeric-function.png","inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.projectimmerse.com\/blog\/phps-is_numeric-function\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.projectimmerse.com\/blog\/phps-is_numeric-function\/","url":"https:\/\/www.projectimmerse.com\/blog\/phps-is_numeric-function\/","name":"PHP's is_numeric function - Project Immerse","isPartOf":{"@id":"https:\/\/www.projectimmerse.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.projectimmerse.com\/blog\/phps-is_numeric-function\/#primaryimage"},"image":{"@id":"https:\/\/www.projectimmerse.com\/blog\/phps-is_numeric-function\/#primaryimage"},"thumbnailUrl":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2018\/08\/php-is-numeric-function.png","datePublished":"2018-08-07T22:30:24+00:00","dateModified":"2020-06-17T02:36:37+00:00","author":{"@id":"https:\/\/www.projectimmerse.com\/blog\/#\/schema\/person\/c53f2864be524ee6fa08a7e4800dd1e5"},"breadcrumb":{"@id":"https:\/\/www.projectimmerse.com\/blog\/phps-is_numeric-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.projectimmerse.com\/blog\/phps-is_numeric-function\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.projectimmerse.com\/blog\/phps-is_numeric-function\/#primaryimage","url":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2018\/08\/php-is-numeric-function.png","contentUrl":"https:\/\/www.projectimmerse.com\/blog\/wp-content\/uploads\/2018\/08\/php-is-numeric-function.png","width":750,"height":750,"caption":"PHP is_numeric function"},{"@type":"BreadcrumbList","@id":"https:\/\/www.projectimmerse.com\/blog\/phps-is_numeric-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.projectimmerse.com\/blog\/"},{"@type":"ListItem","position":2,"name":"PHP&#8217;s is_numeric function"}]},{"@type":"WebSite","@id":"https:\/\/www.projectimmerse.com\/blog\/#website","url":"https:\/\/www.projectimmerse.com\/blog\/","name":"Project Immerse","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.projectimmerse.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.projectimmerse.com\/blog\/#\/schema\/person\/c53f2864be524ee6fa08a7e4800dd1e5","name":"projectimmerse","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4d06955033d6227bfdcf30014e457e4334f7deeb73907de49b65ec2484921931?s=96&d=mm&r=g","caption":"projectimmerse"},"url":"https:\/\/www.projectimmerse.com\/blog\/author\/projectimmerse\/"}]}},"_links":{"self":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts\/211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/comments?post=211"}],"version-history":[{"count":4,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts\/211\/revisions"}],"predecessor-version":[{"id":460,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/posts\/211\/revisions\/460"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/media\/1133"}],"wp:attachment":[{"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/media?parent=211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/categories?post=211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.projectimmerse.com\/blog\/wp-json\/wp\/v2\/tags?post=211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}