Numbers in PHP are used to perform mathematical operations and represent numeric values. PHP supports three main types of numbers:
1️⃣ Integers – Whole numbers (e.g., 10
, -50
, 1000
)
2️⃣ Floats (Doubles) – Decimal numbers (e.g., 3.14
, -2.5
, 0.99
)
3️⃣ Scientific Notation – Numbers using E
or e
(e.g., 2.5e3
for 2500
)
Integer Numbers in PHP
✅ Rules for Integers
✔ Must be a whole number
✔ Can be positive or negative
✔ Must be within the range of -2,147,483,648 to 2,147,483,647 (for 32-bit systems)
✔ No decimal points
Example of an Integer
<?php
$age = 25;
echo $age;
?>
✔ Outputs: 25
Floating-Point Numbers (Decimals)
Floats (or doubles) are numbers that contain decimal points or are written in scientific notation.
Example of a Float
<?php
$price = 99.99;
echo $price;
?>
✔ Outputs: 99.99
Checking If a Variable Is a Number (is_numeric()
)
The is_numeric()
function checks if a value is a number or a numeric string.
Example
<?php
$num1 = 100;
$num2 = "200"; // Numeric string
echo is_numeric($num1); // Outputs 1 (true)
echo is_numeric($num2); // Outputs 1 (true)
?>
PHP supports scientific notation, where E
(or e
) represents powers of 10.
Example
<?php
$largeNumber = 2.5e3; // 2500
echo $largeNumber;
?>
✔ Outputs: 2500
Type Casting: Converting to Integer ((int)
)
You can convert numbers from strings or floats to integers using type casting.
Example
<?php
$floatNum = 15.75;
$intNum = (int)$floatNum; // Converts to 15
echo $intNum;
?>
✔ Outputs: 15
Conclusion
✔ PHP supports integers, floats, and scientific notation.
✔ The is_numeric()
function checks if a value is a valid number.
✔ PHP automatically handles number conversions and operations. 🚀