Yogesh Chauhan's Blog

How to Check If a Variable is an Integer, a Float, a Number, NaN, an Infinite or a Numeric in PHP?

in PHP on November 19, 2019

PHP provides auto data type conversion so you can assign int value in the beginning and change it to float in the middle and PHP won't make a sound!

An integer data type is a non-decimal number between -2147483648 and 2147483647. So, any number out of that range is considered and stored as a float.

So, we need to be careful while assigning values in a lengthy program or calculation as the data type change can cause some errors. 

Here are the functions we can use to check if the number is integer.

  • is_int()
  • is_integer() 
  • is_long()

To check whether the number is float use these following functions:

  • is_float()
  • is_double()

Now, the float has it's own limit as well. If the number is greater than the PHP_FLOAT_MAX than it's infinite number. We can check that using following functions.

  • is_finite()
  • is_infinite()

To check whether the value is not a number, use this

  • is_nan()

Lastly, to check whether the variable is numeric or not, use following function

  • PHP is_numeric()

Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to use data-* Attributes in HTML?HTMLHow to hide and show menu on scroll using JavaScript and CSS?CSSOOP, Class and Objects Strategies For Beginners (PHP)PHPCheck if any column has NULL values in PostgresPostgresuser_can vs current_user_can in WordPressWordPressHow to set up the local environment and workspace for Angular development?AngularHow to Find the Highest (or Lowest) Number in an Array in JavaScript?JavaScriptRevisiting variable scope in JavaScriptJavaScriptUse inline if to make a shorter conditional syntax in ReactReactThe fundamental difference between HAVING and WHERE clauses in PostgresPostgresWhat are Stored Procedures for SQL Server?SQL/MySQLWhat is the best way to add JavaScript Code into HTML?HTML