PHP has three primary types of numbers: Integers, Floats, and Number Strings. Additionally, there are special types like Infinity and NaN.
PHP Numbers
Main Numeric Types in PHP:
- Integer: Whole numbers without decimal points.
- Float: Numbers with decimal points or in exponential form.
- Number Strings: Strings that represent numeric values.
Special Numeric Types in PHP:
- Infinity: Represents values larger than PHP_FLOAT_MAX.
- NaN (Not a Number): Used for invalid mathematical operations.
Creating Variables
To create numeric variables, simply assign values to them:
$age = 25; $price = 399.99; $year = "2025";
Checking Variable Types
Use var_dump() to check the type of any variable:
var_dump($age); var_dump($price); var_dump($year);
PHP Integers
An integer is a whole number without any decimal part. Here are a few examples: 10, -15, 2000. Integers in PHP can be between -2147483648 and 2147483647 on 32-bit systems, and much larger on 64-bit systems.
Rules for Integers:
- Must have at least one digit
- No decimal point allowed
- Can be positive or negative
Constants for Integers:
PHP_INT_MAX: Maximum integerPHP_INT_MIN: Minimum integerPHP_INT_SIZE: Size of an integer in bytes
Functions to Check Integer Type:
is_int()is_integer()(alias ofis_int())is_long()(alias ofis_int())
Example: Checking Integer Type
$engine_capacity = 1200; var_dump(is_int($engine_capacity)); $engine_capacity = 1200.5; var_dump(is_int($engine_capacity));
PHP Floats
A float is a number with a decimal point or in exponential form. Examples: 3.14, 0.001, 2.5E+3.
Constants for Floats:
PHP_FLOAT_MAX: Largest floating point numberPHP_FLOAT_MIN: Smallest positive floating point numberPHP_FLOAT_DIG: Number of decimal digits without precision lossPHP_FLOAT_EPSILON: Smallest positive numberxso thatx + 1.0 != 1.0
Functions to Check Float Type:
is_float()is_double()(alias ofis_float())
Example: Checking Float Type
$mileage = 15.75; var_dump(is_float($mileage));
PHP Infinity
Values larger than PHP_FLOAT_MAX are considered infinite.
Functions to Check Finite/Infinite Values:
is_finite()is_infinite()
Example: Checking Finite/Infinite Value
$speed = 1.8e309; var_dump($speed);
PHP NaN
NaN stands for “Not a Number”. It results from invalid mathematical operations.
Function to Check NaN:
is_nan()
Example: NaN Value
$result = acos(8); var_dump($result);
PHP Numerical Strings
To determine if a variable is numeric, use the is_numeric() function. It returns true if the variable is a number or a numeric string, and false otherwise.
Example: Checking Numeric Strings
$model_number = 7500; var_dump(is_numeric($model_number)); $model_number = "7500"; var_dump(is_numeric($model_number)); $model_number = "75.00" + 100; var_dump(is_numeric($model_number)); $model_number = "Speedster"; var_dump(is_numeric($model_number));
Casting Strings and Floats to Integers
Sometimes you need to convert a value to an integer. Use (int), (integer), or intval() for this purpose.
Example: Casting to Integer
// Cast float to int $engine_capacity = 2345.678; $int_cast = (int)$engine_capacity; echo $int_cast; echo "<br>"; // Cast string to int $engine_capacity = "2345.678"; $int_cast = (int)$engine_capacity; echo $int_cast;


