PHP Numbers

Working with numeric data in PHP

🔢 What are Numbers?

PHP supports integers and floating-point numbers for mathematical operations. Numbers can be positive, negative, or zero and are essential for calculations, counting, and data processing in programs.


<?php
$integer = 42;        // Whole number
$float = 3.14;        // Decimal number
$negative = -10;      // Negative number

echo "Integer: $integer<br>";
echo "Float: $float";
?>
                                    

Output:

Integer: 42

Float: 3.14

Number Types

🔢

Integers

Whole numbers without decimal points. Can be positive, negative, or zero for counting and indexing operations.

<?php
$age = 25;
$year = 2024;
$temp = -5;
?>
💰

Floats

Numbers with decimal points for precise calculations. Also called doubles, used for measurements and financial data.

<?php
$price = 19.99;
$pi = 3.14159;
$rate = 0.05;
?>
🔬

Scientific

Numbers in exponential notation for very large or small values using 'e' notation for scientific calculations.

<?php
$large = 2.5e6;  // 2500000
$small = 1.5e-3; // 0.0015
?>
🎯

Infinity

Special numeric values representing infinite numbers. PHP has constants for positive and negative infinity values.

<?php
$inf = INF;
$negInf = -INF;
echo is_infinite($inf);
?>

🔹 Integer Numbers

Working with whole numbers:

<?php
// Positive integer
$positive = 100;

// Negative integer
$negative = -50;

// Zero
$zero = 0;

// Check if integer
echo "Is $positive an integer? ";
echo is_int($positive) ? "Yes" : "No";
echo "<br>";

// Integer operations
$sum = $positive + $negative;
echo "Sum: $sum<br>";

$product = $positive * 2;
echo "Product: $product";
?>

Output:

Is 100 an integer? Yes

Sum: 50

Product: 200

🔹 Float Numbers

Working with decimal numbers:

<?php
// Float numbers
$price = 29.99;
$pi = 3.14159;
$rate = 0.075;

// Check if float
echo "Is $price a float? ";
echo is_float($price) ? "Yes" : "No";
echo "<br>";

// Float operations
$total = $price * 2;
echo "Total: $" . number_format($total, 2) . "<br>";

// Rounding
$rounded = round($pi, 2);
echo "Pi rounded: $rounded";
?>

Output:

Is 29.99 a float? Yes

Total: $59.98

Pi rounded: 3.14

🔹 Number Checking Functions

Verify number types and properties:

<?php
$int = 42;
$float = 3.14;
$string = "123";

// Check if numeric
echo "Is '$string' numeric? ";
echo is_numeric($string) ? "Yes" : "No";
echo "<br>";

// Check if integer
echo "Is $int an integer? ";
echo is_int($int) ? "Yes" : "No";
echo "<br>";

// Check if float
echo "Is $float a float? ";
echo is_float($float) ? "Yes" : "No";
echo "<br>";

// Check if finite
echo "Is $int finite? ";
echo is_finite($int) ? "Yes" : "No";
?>

Output:

Is '123' numeric? Yes

Is 42 an integer? Yes

Is 3.14 a float? Yes

Is 42 finite? Yes

🔹 Number Formatting

Format numbers for display:

<?php
$number = 1234567.89;

// Format with commas
echo number_format($number) . "<br>";

// Format with 2 decimals
echo number_format($number, 2) . "<br>";

// Custom formatting
echo number_format($number, 2, ".", ",") . "<br>";

// Currency formatting
$price = 1234.56;
echo "$" . number_format($price, 2);
?>

Output:

1,234,568

1,234,567.89

1,234,567.89

$1,234.56

🔹 Rounding Numbers

Round numbers to specific precision:

<?php
$number = 3.14159;

// Round to nearest integer
echo "round(): " . round($number) . "<br>";

// Round to 2 decimals
echo "round(2): " . round($number, 2) . "<br>";

// Round up
echo "ceil(): " . ceil($number) . "<br>";

// Round down
echo "floor(): " . floor($number) . "<br>";

// Absolute value
$negative = -5.5;
echo "abs(): " . abs($negative);
?>

Output:

round(): 3

round(2): 3.14

ceil(): 4

floor(): 3

abs(): 5.5

🔹 Random Numbers

Generate random numbers:

<?php
// Random integer between 1 and 10
$random1 = rand(1, 10);
echo "Random (1-10): $random1<br>";

// Random integer between 1 and 100
$random2 = rand(1, 100);
echo "Random (1-100): $random2<br>";

// Better random number
$random3 = mt_rand(1, 10);
echo "MT Random (1-10): $random3<br>";

// Random float between 0 and 1
$randomFloat = mt_rand() / mt_getrandmax();
echo "Random float: " . round($randomFloat, 2);
?>

Output:

Random (1-10): 7

Random (1-100): 42

MT Random (1-10): 3

Random float: 0.65

🧠 Test Your Knowledge

Which function rounds a number up to the nearest integer?