PHP Numbers


We will examine in-depth the entities, floats, and string numbers in this chapter.

One thing to keep in mind concerning PHP is that it has built-in data type conversion.

As a result, if you give a variable an integer value, the variable's type will be an integer as well. The type will change to a string if you assign a string to the same variable.

It's possible that this automated conversion will damage your code.

You can also search for these topics, php numbers, numbers using php, php number validation, number conversion in php, number types in php.

PHP Integers

2, 256, -256, 10358, -179567 are all integers.

A number with no decimal portion is known as an integer.

An integer data type is a non-decimal number between -2147483648 and 2147483647 in 32 bit systems, and between -9223372036854775808 and 9223372036854775807 in 64 bit systems.

A bigger (or lower) number, since it exceeds the integer's limit, should be saved as the float.

Note :- Also vital to be aware that although 4* 2.5 is 10, the result is stored as a float as one of the operands is a float (2.5).

Here are some rules for integers :-

  • A single-digit is required for an integer.
  • An integer cannot contain a decimal point.
  • A whole might be positive or negative.
  • Entities might be defined in decimal (base 10), hexadecimal (base 16), octal notation (base 8), or binary notation (base 2).

PHP has the following predefined constants for integers:

  • PHP_INT_MAX - The largest integer supported
  • PHP_INT_MIN - The smallest integer supported
  • PHP_INT_SIZE - The size of an integer in bytes

PHP has the following functions to check if the type of a variable is integer:

  • is_int()
  • is_integer() - alias of is_int()
  • is_long() - alias of is_int()

Example :- Check if the type of a variable is integer :

<?php
$x = 5985;
var_dump(is_int($x));

$x = 59.85;
var_dump(is_int($x));
?>

Output :-

bool(true)
bool(false)

You can also search for these topics, integers using php, php integer example, php integer variable, php integer declaration, rules to define integer using php, php type of variable integer, pre-defined constant integers in php.

PHP Floats

A float is an exponentially formed number with a decimal dot or number.

2.0, 256.4, 10.358, 7.64E+5, 5.56E-5 are all floats.

The float data type can generally hold values up to 1,7976931348623E+308 (depending on platform) with a maximum accuracy of 14 digits.

For floats, PHP has the following built-in constants (from PHP 7.2).

  • PHP_FLOAT_MAX - The largest representable floating point number.
  • PHP_FLOAT_MIN - The smallest representable positive floating point number.
  • -PHP_FLOAT_MAX - The smallest representable negative floating point number.
  • PHP_FLOAT_DIG - The number of decimal digits that can be rounded into a float and back without precision loss.
  • PHP_FLOAT_EPSILON - The smallest representable positive number x, so that x + 1.0 != 1.0.

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

  • is_float()
  • is_double() - alias of is_float()

Example :- Check if the type of a variable is float:

<?php
$x = 10.365;
var_dump(is_float($x));

$y = 1.3e5;
var_dump(is_float($y));
?>

Output :-

bool(true)
bool(true)

You can also search for these topics, float using php, php float example, php float value, php float function, php float typecast, php float precision, php float range, php float variable types, php float constants.

PHP Infinity

The value is infinite and significantly greater than PHP FLOAT MAX.

PHP includes the methods below to verify whether a number is endless or infinite :

  • is_finite()
  • is_infinite()

The method var_dump() returns the data type and value, other hand.

Example :- Check if a numeric value is finite or infinite :

<?php
$x = 1.9e411;
var_dump($x);
?>

Output :-

float(INF)

You can also search for these topics, infinity using php, php infinity example, php infinity numeric value, infinity php hosting, php infinty symbol.

PHP NaN

Not a Number is not as NaN.

NaN is used for mathematical operations which cannot be carried out.

PHP includes these features to verify whether a value is not a number :

  • is_nan()

Example :- Invalid calculation will return a NaN value :

<?php
$x = acos(8);
var_dump($x);
?>

Output :-

float(NAN)

You can also search for these topics, NaN using php, php nan value, php nan check, php nan error, php nan example, php nan return value, php nan constant.

PHP Numerical Strings

To determine whether a variable is numeric, use the PHP is_numeric() function. If the variable is a number or a numeric string, the function returns true; otherwise, it returns false.

Example :- Check if the variable is numeric:

<?php
$x = 5985;
var_dump(is_numeric($x));

$x = "5985";
var_dump(is_numeric($x));

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

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

Output :-

bool(true)
bool(true)
bool(true)
bool(false)

You can also search for these topics, numeric strings using php, php is_numeric() function, php numerical string example, php numerical string value, php numerical string as key.

PHP Casting Strings and Floats to Integers

You may need to convert a numerical number to a different data type on occasion.

Converting a value to an integer is usually done with the (int), (integer), or intval() functions.

Example :- Cast float and string to integer :

<?php

// Cast float to int
$x = 23465.768;
$int_cast = (int)$x;
echo $int_cast;

echo "<br />";

// Cast string to int
$x = "23465.768";
$int_cast = (int)$x;
echo $int_cast;
?>

Output :-

23465
23465

Note:- The integer datatype does not support decimal values.

You can also search for these topics, php casting strings and float to integers, php casting strings to int, php casting string to float, php string casting types, php casting types example.