In PHP, numbers are primarily categorized as either Integers (whole numbers) or Floats (numbers with a decimal component). PHP provides a full suite of functions to perform complex calculations, format numbers, and validate numeric data.
1. PHP Numbers: Integers vs. Floats
| Type | Description | Example |
| Integer | A non-decimal number (whole number) between -2,147,483,648 and 2,147,483,647 (on 32-bit systems). | $age = 45; |
| Float | A number with a decimal point, or a number in exponential form (often used for scientific notation). | $price = 19.99; $scientific = 1.5e3; |
Checking the Type
PHP provides simple functions to determine if a variable is numeric, which is often needed for validation of user input (from the “Forms” chapter).
is_int(): ReturnsTRUEif the variable is an integer.is_float(): ReturnsTRUEif the variable is a float.is_numeric(): ReturnsTRUEif the variable is a number or a numeric string (like"59.5").
Example:
<?php
$a = 59;
$b = "10.2";
var_dump(is_int($a)); // Output: bool(true)
var_dump(is_float($a)); // Output: bool(false)
var_dump(is_numeric($b)); // Output: bool(true) - because it's a numeric string
?>
2. PHP Math Constants and Basic Functions
PHP offers many built-in constants and functions for common mathematical operations.
| Function/Constant | Purpose | Example | Result |
pi() | Returns the value of PI (3.14159…). | echo pi(); | 3.1415926535898 |
min() | Finds the lowest value in a list of arguments. | min(10, 5, 25); | 5 |
max() | Finds the highest value in a list of arguments. | max(10, 5, 25); | 25 |
abs() | Returns the absolute (positive) value of a number. | abs(-15); | 15 |
sqrt() | Returns the square root of a number. | sqrt(64); | 8 |
3. Rounding Numbers (Crucial for Currency)
When dealing with money or precision, you need control over rounding.
| Function | Purpose | Example | Result |
round() | Rounds a float to the nearest whole number, or to a specified precision. | round(3.4); round(3.456, 1); | 3 |
ceil() | Rounds a number up to the nearest integer (ceiling). | ceil(4.1); ceil(4.9); | 5 |
floor() | Rounds a number down to the nearest integer (floor). | floor(4.1); floor(4.9); | 4 |
Example:
<?php
$cost = 12.378;
// Round to 2 decimal places for currency display
$formatted_cost = round($cost, 2);
echo "Cost: $" . $formatted_cost . "<br>"; // Output: $12.38
// Rounding up the price
echo "Next whole dollar: " . ceil($cost); // Output: 13
?>
4. Number Formatting
While round() handles math, the number_format() function is used solely for displaying numbers with correct thousands separators and decimal points, which varies by country.
| Syntax | Description |
number_format(number, decimals, decimal_sep, thousands_sep) | Formats the number according to the rules provided. |
Example (US vs. European Format):
<?php
$large_number = 14500.75;
// US Format: (14,500.75) - period for decimal, comma for thousands
$us_format = number_format($large_number, 2, '.', ',');
echo "US Format: " . $us_format . "<br>";
// European Format: (14.500,75) - comma for decimal, period for thousands
$eu_format = number_format($large_number, 2, ',', '.');
echo "EU Format: " . $eu_format;
?>
Next Steps (The Final Foundation)
You have now covered all the core building blocks necessary for writing procedural PHP code: Syntax, Data Types, Logic, Arrays, Strings, Dates, and Math.
The next major chapter must introduce Object-Oriented Programming (OOP), which is the industry-standard way to write modern, organized, and scalable PHP applications. But before that let’s see “PHP File Handling“.
