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 How to check if radio button is checked or not using JavaScript? #2 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#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 #Jan 14 How to display a student’s individual transcript in Colleague? #Jan 11 How to install PuTTY on a MacOS?
You might also like these
How to remove special characters (dash, asterisk etc) from any string in PHP?PHPHow to Secure Web or Mobile Browsers?MiscellaneousIntroduction to Angular modules Part 2: NgModules and componentsAngularHow to get query string values in JavaScript? (URL Manipulation using URLSearchParams)JavaScriptDid you get an error while displaying Special Characters in JavaScript?JavaScriptKilling A Project Part 4: What Would Be Your Top 3 Priorities For Planning The Cancellation?MiscellaneousGeneral concepts in DrupalDrupalQuery to increment or decrement value in MySQL ignoring negative valuesSQL/MySQLHow to use @each rule with key-value pair in SCSS?SCSSHow to make a UILabel clickable in Swift 5?SwiftHow to create a Random Hex Color generator using JavaScript?JavaScriptWhat does JSX do in React?React