In this chapter
We'll learn how to find and read PHP's built-in functions — the signature on php.net, optional parameters, references and false-on-failure returns — the money helpers intdiv(), round() and number_format(), how function_exists() checks what's available, and why including two files that both declare formatPrice() is a fatal error.
The Problem in Real Life
The receipt page shows the checkout summary and the invoice lines, so it includes both checkout-helpers.php and invoice-helpers.php. The moment Mia opens it, the page dies: "Fatal error: Cannot redeclare function formatPrice()".
Both helper files declare their own formatPrice() — the same function, copied once more. While she's there, Mia notices the invoice copy uses round($pence / 100) and shows £28 for a £27.50 booking.
It's the same function twice. Why can't PHP just use one of them?
Mia
Writing Your Own vs. Using PHP's
Thousands of functions
PHP ships with thousands of functions. Most problems — rounding, formatting, searching — are already solved.
Reading the signature
The manual's one-line signature tells you every parameter, its type, its default and what comes back.
One name, one function
PHP keeps one global list of function names. Declaring a name twice — or a built-in's name — is fatal.
Built-In Functions
PHP's own library: before you write a function, check whether PHP already has one. This chapter covers:
- Built-in functions: what's there, and how to read the manual.
- Money helpers:
intdiv(),round()andnumber_format(). - Function existence:
function_exists(). - Redeclaration: why a function name can only be declared once.
Built-in functions: PHP comes with thousands of functions, grouped into extensions. The core ones — strings, arrays, maths, dates — are always there. Others, like mbstring (Act 07) or intl, are switched on or off per installation (Act 01). You've already used a few dozen: strlen(), count(), sprintf(), array_map(). A good habit: before writing a helper, search the PHP manual at php.net for the job ("php round to 2 decimals", "php array unique") — there's very often a function for it, tested by millions of users.
Reading a signature: every function in the manual starts with its signature: number_format(float $num, int $decimals = 0, ?string $decimal_separator = ".", ?string $thousands_separator = ","): string. Read it like your own declarations: parameter types and names, defaults for the optional ones, and the return type. An & before a parameter means it's taken by reference (chapter 4), like preg_match()'s &$matches. A return type like int|false or string|false means "this, or false when it fails" — check for false with ===. And because the parameters have names, named arguments work with built-ins too: number_format(27.5, decimals: 2).
<?phpecho number_format(1234.5, decimals: 2), "\n"; // 1,234.50echo number_format(1234.5, decimals: 2, thousands_separator: " "), "\n"; // 1 234.50$position = strpos("VIP - fade", "Regular"); // returns int|falsevar_dump($position === false); // bool(true): check failure with ===
Money helpers: with prices in pence, three functions do most of the work. intdiv($a, $b) divides two integers and drops the remainder, which is how this Act calculates "10%, rounded down": intdiv($pence * 10, 100); % gives the remainder. round($x, $decimals) rounds to the nearest value; halves go away from zero, so round(2.5) is 3 and round(-2.5) is -3. Pass PHP_ROUND_HALF_EVEN (or, since PHP 8.4, RoundingMode::HalfEven) to round halves to the even neighbour, as banks often do. number_format() turns pence into the text people read. That was the invoice bug: round($pence / 100) rounds to whole pounds. The fix is number_format($pence / 100, 2) — or, better, the one shared formatPrice().
<?php$pence = 2750;echo intdiv($pence * 10, 100), "\n"; // 275: 10%, rounded downecho round($pence / 100), "\n"; // 28 — the invoice bug: whole poundsecho number_format($pence / 100, 2), "\n"; // 27.50var_dump(round(2.5), round(-2.5)); // float(3) float(-3): away from zerovar_dump(round(2.5, 0, PHP_ROUND_HALF_EVEN)); // float(2): to the even neighbourvar_dump(round(3.5, 0, RoundingMode::HalfEven)); // float(4): the PHP 8.4 enum
function_exists: function_exists("mb_strlen") returns true if a function with that name is available right now — a built-in from a loaded extension, or one of yours that has already been declared. It's useful for code that must run on servers where an extension may be missing (Act 01 used extension_loaded() for the same purpose).
<?phpfunction formatPrice(int $pence): string{return "£" . number_format($pence / 100, 2);}var_dump(function_exists("number_format")); // bool(true): core PHPvar_dump(function_exists("mb_strlen")); // bool(true): the mbstring extension is loadedvar_dump(function_exists("formatPrice")); // bool(true): declared abovevar_dump(function_exists("FORMATPRICE")); // bool(true): names are case-insensitivevar_dump(function_exists("calculatePrise")); // bool(false)
Redeclaration: PHP keeps one global list of function names, and each name can be declared only once per request. Names are case-insensitive, so formatPrice and FormatPrice are the same name. Declaring a name that's taken — by your own function or a built-in like round — is a fatal error that stops the page before it starts. On the receipt page, two included files each declared formatPrice(). The real fix is the lesson of this whole Act: one function in one file that every page includes, loaded with require_once so it's only loaded once (Act 13). In older code you'll see each declaration wrapped in if (!function_exists("formatPrice")) { ... }, which hides the duplicate instead of removing it. Act 13's namespaces also let two libraries both have a formatPrice() without clashing. The table below lists the helpers from this chapter.
| Function | Does | Example and result |
|---|---|---|
| intdiv($a, $b) | Whole-number division, remainder dropped | intdiv(27500, 100) gives 275 |
| $a % $b | The remainder | 7 % 2 gives 1 |
| round($x, $decimals) | Rounds; halves away from zero | round(2.5) gives 3.0 |
| round($x, 0, PHP_ROUND_HALF_EVEN) | Halves to the even neighbour | round(2.5, 0, PHP_ROUND_HALF_EVEN) gives 2.0 |
| number_format($n, 2) | Text with 2 decimals and thousands separators | number_format(2750 / 100, 2) gives "27.50" |
| function_exists($name) | Is this function available? | function_exists("mb_strlen") gives true |
| extension_loaded($name) | Is this extension loaded? | extension_loaded("mbstring") gives true |
<?php// What the receipt page sees after including both helper files:function formatPrice(int $pence): string // from checkout-helpers.php{return "£" . number_format($pence / 100, 2);}function formatPrice(int $pence): string // from invoice-helpers.php{return "£" . round($pence / 100);}// Fatal error: Cannot redeclare function formatPrice() (previously declared in main.php:4)
<?phpfunction round(float $value): float{return $value;}// Fatal error: Cannot redeclare function round()
Key Takeaway
PHP has thousands of built-in functions — search php.net before writing your own, and read the signature: types, defaults, & for references, and |false for failure. For money in pence, use intdiv() for whole-number division, round() (halves away from zero, or HalfEven) and number_format() for display. Function names are global and case-insensitive: declaring one twice is fatal, so keep each function in one file and include it once.
Why This Matters
Every function you don't write is a function you don't have to test or fix. PHP's built-ins handle the tricky parts — rounding halves, thousands separators, multibyte text — the same way on every server. Reading their signatures is a skill you'll use every working day.
And the "Cannot redeclare" error is PHP enforcing the lesson of this Act: one name, one function. The fix is never a second copy; it's one shared file.
There's now one prices.php with one formatPrice(), included once by every page, and the invoice shows £27.50 again. Before launch, Marcus asks Mia to review the final price code. The app sent "hairkut" yesterday; the price function printed "Unknown service" and returned nothing, and the booking went through for £0. That's the last chapter: never, generators and good function design.
