Skip to main content

PHP Numbers

By SamK
0
0 recommends
Topic(s)

In this tutorial, you will learn what are integers, floats, and numeric strings in PHP.

PHP Numbers

PHP features three primary numeric data types:

  • Integer
  • Float
  • Number Strings

Additionally, PHP supports two more data types for special numeric cases:

  • Infinity
  • NaN

Numeric type variables are instantiated when assigned a value.

<?php
$integerValue = 10;
$floatValue = 8.67;
$stringValue = "50";

var_dump($integerValue);
var_dump($floatValue);
var_dump($stringValue);
?>

/*
Output:
int(10)
float(8.67)
string(2) "50" 
*/

PHP Integers

2, 256, -256, 10358, and -179567 are all examples of integers.

An integer is a whole number without any decimal part.

In PHP, an integer data type represents non-decimal numbers. In 32-bit systems, integers range between -2147483648 and 2147483647, while in 64-bit systems, the range extends from -9223372036854775808 to 9223372036854775807. If a value exceeds these limits, it will be stored as a float data type, as it surpasses the integer limit.

It's important to note that even though the result of 4 * 2.5 is 10, the result is stored as a float because one of the operands is a float (2.5).

Here are some guidelines for integers in PHP:

  • An integer must contain at least one digit.
  • Integers must not include a decimal point.
  • Integers can be positive or negative.
  • Integers can be specified in various formats: decimal (base 10), hexadecimal (base 16, prefixed with 0x), octal (base 8, prefixed with 0), or binary (base 2, prefixed with 0b).

PHP provides the following predefined constants for integers:

  • PHP_INT_MAX: The maximum supported integer value.
  • PHP_INT_MIN: The minimum supported integer value.
  • PHP_INT_SIZE: The size of an integer in bytes.

To check if a variable is of type integer, PHP offers the following functions:

  • is_int()
  • is_integer() (an alias of is_int())
  • is_long() (also an alias of is_int())

To verify whether the variable's type is an integer, you can use the below code.

<?php
$number = 1000;
var_dump(is_int($number));

$decimalNumber = 10.5;
var_dump(is_int($decimalNumber));
?>

/*
Output:
bool(true)
bool(false)
*/

PHP Floats

A float represents a number with a decimal point or in exponential form.

Examples of floats include 2.0, 256.4, 10.358, 7.64E+5, and 5.56E-5.

Floats in PHP can typically store values up to approximately 1.7976931348623E+308 (platform dependent) with a maximum precision of 14 digits.

PHP introduced the following predefined constants for floats starting from version 7.2:

  • PHP_FLOAT_MAX: The largest representable floating-point number.
  • PHP_FLOAT_MIN: The smallest representable positive floating-point number.
  • PHP_FLOAT_DIG: The maximum number of decimal digits that can be rounded into a float and back without loss of precision.
  • PHP_FLOAT_EPSILON: The smallest representable positive number 'x' such that 'x + 1.0' does not equal '1.0'.

PHP provides the following functions to check if the type of a variable is float:

  • is_float()
  • is_double() (an alias of is_float())

To verify whether the variable's type is a float, you can use the below code.

<?php
$number = 15.75;
var_dump(is_float($number));
?>

/*
Output:
bool(true)
*/

PHP Infinity

PHP treats a numeric value as infinite if it exceeds PHP_FLOAT_MAX.

To ascertain whether a numeric value is finite or infinite, PHP offers the following functions:

  • is_finite()
  • is_infinite()
<?php 
echo var_dump(is_finite(10)) . "<br>"; 
echo var_dump(is_finite(log(0))) . "<br>"; 
echo var_dump(is_infinite(10)) . "<br>"; 
echo var_dump(is_infinite(log(0))); 
?>

/*
Output:
bool(true)
bool(false)
bool(false)
bool(true)
*/

PHP NaN

NaN, which stands for "Not a Number," is employed to represent impossible mathematical operations.

To verify if a value is not a number, PHP offers the is _nan() function.

Nevertheless, the var_dump() function in PHP presents both the data type and value:

An invalid calculation will result in a NaN (Not a Number) value.

<?php
$result = asin(1.5);
var_dump($result);
?>

/*
Output:
float(NAN)
*/

PHP Numerical Strings

The PHP function is_numeric() serves to determine if a variable is numeric. It yields true if the variable is a number or a numeric string; otherwise, it returns false.

<?php
$integerValue = 1234;
var_dump(is_numeric($integerValue));

$stringNumber = "1234";
var_dump(is_numeric($stringNumber));

$calculatedValue = "45.67" + 200;
var_dump(is_numeric($calculatedValue));

$nonNumericString = "World";
var_dump(is_numeric($nonNumericString));
?>

/*
Output:
bool(true)
bool(true)
bool(true)
bool(false)
*/

Note: Starting from PHP 7.0, the is_numeric() function will return FALSE for numeric strings in hexadecimal form (e.g., 0xf4c3b00c), as they are no longer considered numeric strings.

PHP Casting Strings and Floats to Integers

Occasionally, you may need to convert a numerical value into another data type.

The (int), (integer), and intval() functions are commonly employed for converting a value into an integer.

Convert a float or string to an integer using casting.

<?php
// Cast float to int
$floatValue = 12345.678;
$intCast = (int)$floatValue;
echo $intCast;

echo "<br>";

// Cast string to int
$stringValue = "98765.432";
$intCast = (int)$stringValue;
echo $intCast;
?>

/*
Output:
12345
98765
*/

Questions & Answers