data types and standard library

Numbers

PHP mainly gives you integers and floating-point numbers. Integers are exact whole numbers. Floats are approximate decimal numbers. That difference matters.

Integers

PHP example
<?php

declare(strict_types=1);

$quantity = 3;
$unitPricePennies = 1299;
$lineTotalPennies = $quantity * $unitPricePennies;

echo $lineTotalPennies;

// Prints:
// 3897

Integers are the right choice for counts, quantities, and money stored in the smallest unit, such as pennies or cents.

Money should usually use integer minor units

Do calculations in integer pennies, then format for display at the boundary.

PHP example
<?php

declare(strict_types=1);

$priceInPennies = 1299;
$quantity = 3;
$totalInPennies = $priceInPennies * $quantity;

echo 'GBP ' . number_format($totalInPennies / 100, 2) . PHP_EOL;

// Prints:
// GBP 38.97

This is safer than doing financial calculations with floating-point pounds throughout the program.

Floats are approximate

Floats are useful for measurements and ratios, but they are not exact decimal money values.

PHP example
<?php

declare(strict_types=1);

var_dump(0.1 + 0.2);

// Output is a float close to 0.3, not a decimal-money guarantee.

Do not compare important float calculations as if they were exact strings or exact money values. If exact decimal arithmetic is required, use integer minor units or a decimal/math library designed for that job.

Division

The / operator returns a float. Use intdiv() when you need integer division.

PHP example
<?php

declare(strict_types=1);

var_dump(5 / 2);
var_dump(intdiv(5, 2));

// Prints:
// float(2.5)
// int(2)

Guard against division by zero before calling / or intdiv().

PHP example
<?php

declare(strict_types=1);

function averagePennies(int $totalPennies, int $count): int
{
    if ($count === 0) {
        throw new InvalidArgumentException('Count must be greater than zero.');
    }

    return intdiv($totalPennies, $count);
}

echo averagePennies(1000, 4);

// Prints:
// 250

Rounding

Rounding should happen deliberately, close to the rule that requires it.

PHP example
<?php

declare(strict_types=1);

$average = 10 / 3;

echo number_format($average, 2);

// Prints:
// 3.33

number_format() is for display. Do not confuse display formatting with internal calculation.

Numeric strings from input

Form and query-string input usually arrives as strings. Validate and convert at the boundary.

PHP example
<?php

declare(strict_types=1);

function parseQuantity(string $value): int
{
    if (! ctype_digit($value)) {
        throw new InvalidArgumentException('Quantity must be a whole number.');
    }

    $quantity = (int) $value;

    if ($quantity < 1) {
        throw new InvalidArgumentException('Quantity must be at least 1.');
    }

    return $quantity;
}

echo parseQuantity('3');

// Prints:
// 3

Do not let unvalidated strings drift deep into calculation code.

Overflow and large values

PHP integers have limits based on the platform. Most web application values will not approach those limits, but imports, IDs, timestamps, and financial aggregates can become large.

Use clear names, validate ranges, and be careful when multiplying large values.

What to remember

Use integers for counts and money in minor units. Use floats for approximate measurements. Validate numeric strings at the boundary. Guard division by zero. Format numbers for display only at the output boundary.

Before moving on, make sure you can explain why 1299 pennies is safer for calculations than 12.99 pounds.

Practice

Task: Calculate a line total

Write a small price calculator that keeps money as integer pennies.

Requirements

  • Use strict types.
  • Create a lineTotalPennies() function.
  • Accept unit price in pennies and quantity as integers.
  • Reject quantities lower than 1.
  • Return the line total as integer pennies.
  • Format the final result for display as GBP 38.97.
  • Include one normal case and one invalid quantity case.
Show solution
PHP example
<?php

declare(strict_types=1);

function lineTotalPennies(int $unitPricePennies, int $quantity): int
{
    if ($quantity < 1) {
        throw new InvalidArgumentException('Quantity must be at least 1.');
    }

    return $unitPricePennies * $quantity;
}

function formatMoney(int $pennies): string
{
    return 'GBP ' . number_format($pennies / 100, 2);
}

echo formatMoney(lineTotalPennies(1299, 3)) . PHP_EOL;

try {
    lineTotalPennies(1299, 0);
} catch (InvalidArgumentException $exception) {
    echo $exception->getMessage() . PHP_EOL;
}

// Prints:
// GBP 38.97
// Quantity must be at least 1.

The calculation uses integer pennies. Formatting happens only when printing the result.