PHP Numbers

In this chapter we will discuss PHP numbers in detail and also about functions related PHP numbers.

PHP Numbers: Integer

Integers can only store whole numbers with either positive or negative signs, such as, numbers without a fractional part or a decimal point.

Rules for Integer
  • Integer can be a positive or negative whole number.
  • Integer should not contain decimal point or fraction.
  • At least one digit is needed for an integer.
  • An integer’s range must be between -2,147,483,648 and 2,147,483,647, or -2^31 and 2^31 respectively.
  • Decimal (base 10) notation, hexadecimal (base 16), octal (base 8) notation, and binary (base 2) notation, these all can be used to specify integers.
Predefined Constants for Integer
  • PHP_INT_MAX: It is predefined constant that represents the largest integer value that can be used, Basically it returns the largest supported integer by specific PHP version.
  • PHP_INT_MIN: It is predefined constant that represents the smallest integer value that can be used, Basically it returns the smallest supported integer by specific PHP version.
  • PHP_INT_SIZE: It is a predefined constant that represents integer size in bytes.
<?php
 // It will return max, min limit of php int and size of php int in bytes
echo PHP_INT_MAX. "</br>";
echo PHP_INT_MIN. "</br>";
echo PHP_INT_SIZE;
?>
Functions for checking Integer:

To determine if a variable’s form is integer, PHP provides the following functions:

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

The above three functions have same operation, they check whether the type of variable is integer or not.

<?php
// Check Wether type of a variable is integer or not  
$x = 5.55;
var_dump(is_int($x));//In output it will also tell type of this function

echo "<br>";

// Check again... 
$x = 5;
var_dump(is_integer($x));
?>   

PHP Floats

Float can hold numbers with a fractional or decimal point, as well as it can be negative or positive, such as 2.4, 555.64 , 5.5+6 these all are floating point.

Rules for Float:
  • It can be a positive or negative decimal number.
  • It can not contain fraction.
  • It will typically store a value up to 1.7976931348623E+308, but it depends on platform as well.
  • It have a maximum precision of 14 digits.
Predefined Constants for Float:
  • PHP_FLOAT_MAX: It is predefined constant that represents the largest floating value that can be used, Basically it returns the largest supported float by specific PHP version.
  • PHP_FLOAT_MIN: It is predefined constant that represents the smallest floating value that can be used, Basically it returns the smallest supported floating number by specific PHP version.
  • – PHP_FLOAT_MAX: It is predefined constant that represents the smallest negative floating value that can be used, Basically it returns the smallest negative supported floating number by specific PHP version.
  • PHP_FLOAT_DIG: It is predefined constant that represents the number of decimal digits that can be rounded to and from a float without losing precision.
  • PHP_FLOAT_EPSILON: It is predefined constant that represents smallest representable positive number x, such as x + 1.0 != 1.0 .
Functions for checking Floating Number:
  • is_float()
  • is_double()

The above functions have same operation, they check whether the type of variable is float or not.

<?php
// Check Wether type of a variable is float or not  
$x = 5.55;
var_dump(is_float($x));//In output it will also tell type of this function

echo "<br>";

// Check again... 
$x = 5;
var_dump(is_double($x));
?>   

PHP Infinity

PHP Infinity is defined as a numeric value greater than PHP FLOAT MAX.

Functions for checking Infinity:
  • is_finite()
  • is_infinite()

The above functions have same operation, they check whether the type of variable is infinite or finite.

<?php
// Check if a numeric value is finite or infinite 
$x = 1.9e411;
var_dump(is_infinite($x));
echo "<br>";
var_dump(is_finite($x));
echo "<br>";
var_dump($x);
?> 

PHP NAN

Basically, NA is abbreviation of not a number. For mathematical operations that are difficult to perform, NaN is used.

is_nan() is a function that checks whether the value is number or not.

<?php
// Invalid calculation will return a NaN value
$x = acos(2);
var_dump($x);
echo "</br>";
$x = acos(0);// it is a Number so it will return output 
var_dump($x);
?> 

PHP Numerical Strings:

PHP is numeric is used to determine if a variable is numeric or not. If the variable is a number or a numeric string, the function returns true; otherwise, it returns false.

<?php
// Check if the variable is numeric   
$x = 5985;
var_dump(is_numeric($x));

echo "<br>";

$x = "5985";// Still it will be considered as numeric 
var_dump(is_numeric($x));

echo "<br>";

$x = "59.85" + 100;
var_dump(is_numeric($x));

echo "<br>";

$x = "Hi";
var_dump(is_numeric($x));
?>