PHP Numbers

In PHP, there are two types of numbers: integers and floats.

An integer is a whole number, without a decimal point, like 4195. You can use the following functions to work with integers:

  • is_int() – Returns true if the value is an integer, false otherwise.
  • intval() – Converts a value to an integer.
  • (int) – Typecasts a value to an integer.

A float is a number with a decimal point, like 3.14159. You can use the following functions to work with floats:

  • is_float() – Returns true if the value is a float, false otherwise.
  • floatval() – Converts a value to a float.
  • (float) – Typecasts a value to a float.

Here are some examples of how you can use these functions:

$int1 = 123;
$int2 = '123';
$int3 = '123.45';

var_dump(is_int($int1)); // Outputs: bool(true)
var_dump(intval($int2)); // Outputs: int(123)
var_dump((int)$int3); // Outputs: int(123)

$float1 = 123.45;
$float2 = '123.45';
$float3 = '123';

var_dump(is_float($float1)); // Outputs: bool(true)
var_dump(floatval($float2)); // Outputs: float(123.45)
var_dump((float)$float3); // Outputs: float(123)

PHP NaN

In PHP, the “Not a Number” (NaN) value represents the result of an invalid mathematical operation, such as dividing zero by zero. The NaN value is not equal to any other value, including itself, so you cannot use comparison operators (such as == or ===) to test if a value is NaN.

Instead, you can use the is_nan() function to check if a value is NaN. For example:

$result = 0 / 0;

if (is_nan($result)) {
  echo 'The result is NaN';
}

You can also use the nan() function to create a NaN value. For example:

$nan = nan();

if (is_nan($nan)) {
  echo 'This is a NaN value';
}