PHP Numeric Data Types

Aug 14, 2009 Author: City Hall

PHP assigns a data type to each value and that the numeric data types are integer and double, for whole numbers.

To check whether a value is either of these types, you use the is_float and is_int functions. Likewise, to check for either numeric data type in one operation, you can use is_numeric.

The following example contains a condition that checks whether the value of $number is an integer:

$number = "28";
if (is_int($number)) {  
 echo "$number is an integer";
 echo "$number is not an integer";

Because the actual declaration of that variable assigns a string valuealbeit one that contains a numberthe condition fails.

Although $number in the previous example is a string, PHP is flexible enough to allow this value to be used in numeric operations. The following example shows that a string value that contains a number can be incremented and that the resulting value is an integer:

$number = "6";
echo "$number has type " . gettype($number);

views 5133
  1. Add New Comment