Why not just cast?
PHP will read anything as anything if you cast. (int) on a string, (bool) on a flag,
strtotime on whatever the form sent — nothing stops, nothing warns, and a value comes
back every time. That is exactly how a form field becomes a wrong number nobody notices
until it is on an invoice. Each section below is one of those casts, the value it quietly
gets wrong, and the library's reader beside it, which refuses the same value and says why.
They build on each other, so start at the top.
(bool) makes "false" true
Start with the smallest cast there is. PHP's own idea of true and false is about emptiness,
not meaning: a string that holds anything is true, whatever it says, and '0' is the one
string that is false. So a cast, an if ($value) or a ?: reads a flag column backwards,
and empty() throws away a quantity of zero as if nobody had typed it:
use SafeCoder\CommonKit\CkBool;
(bool) 'false'; // -> true (a string with something in it)
(bool) 'no'; // -> true
(bool) '0'; // -> false
empty('0'); // -> true (a real zero, read as "nothing there")
CkBool::FromValue('false'); // -> false
CkBool::FromValue('no'); // -> false
CkBool::FromValueOrNull('0'); // -> false (said no)
CkBool::FromValueOrNull('maybe'); // -> null (said nothing readable)
Read a flag through CkBool and never through a cast. An HTML checkbox
is the everyday case: it posts on when ticked and nothing at all when not, which
CkBool::FromTextOrNull reads as true or null:
use SafeCoder\CommonKit\CkBool;
CkBool::FromTextOrNull(
input: 'on',
trueWords: ['on']
); // -> true (ticked)
CkBool::FromTextOrNull(
input: null,
trueWords: ['on']
); // -> null (unticked: the field was not posted)
(int) reads the digits it finds and stops
Numbers are no better. A cast and intval() take whatever digits a value starts with and
drop the rest, so junk reads as a number and nobody is told — and a value with no digits at
all reads as the same 0 a real zero does:
use SafeCoder\CommonKit\CkNumber;
(int) '12abc'; // -> 12 (the letters are dropped)
(int) '1,234'; // -> 1 (and so are the digits after the comma)
(int) '12.9'; // -> 12 (cut, not rounded)
intval('$5'); // -> 0 (indistinguishable from a real zero)
CkNumber::Integer('12abc'); // -> 0 (rejected: the default fallback)
CkNumber::IntegerOrNull('12abc'); // -> null (rejected, and it says so)
CkNumber::Integer('12.9'); // -> 13 (a fraction is rounded, to the nearest)
CkNumber::Explain('12abc'); // -> 'not a number'
A value is a number to CkNumber when the whole of it is one.
Anything else is rejected — to the fallback, to null from the OrNull twin, or with its
reason from Explain.
"$5" and "1,234" are not numbers
That strictness cuts the other way too, and it is meant to. CkNumber
reads plain invariant numbers — digits, one decimal point, an optional sign — and nothing
else. Currency symbols and digit grouping (the commas in 1,234) are money dressing, and
money is CkMoney's job:
use SafeCoder\CommonKit\CkMoney;
use SafeCoder\CommonKit\CkNumber;
CkNumber::Integer('1,234'); // -> 0 (rejected, the default fallback)
CkNumber::Decimal('$5'); // -> '0' (rejected)
CkMoney::Amount('$5'); // -> 5.0
CkMoney::Amount('$1,234.56'); // -> 1234.56
The split is deliberate: a comma in a "number" is ambiguous (grouping in the US,
decimals in Europe), so CkNumber refuses it and CkMoney::Amount resolves it with
its mark parameter.
strtotime guesses
Dates are where a guess does the most damage, and strtotime() and new DateTime('…')
do nothing but guess: they read whatever they are given as generously as they can — a day
that does not exist rolls into the next month, a two-digit year is given a century, and
5/6 is read as May the 6th because that is how America writes it — all without a word.
A date that is wrong looks exactly like one that is right:
use SafeCoder\CommonKit\CkDate;
use SafeCoder\CommonKit\Enums\DateOrder;
date('Y-m-d', strtotime('2026-02-31')); // -> '2026-03-03' (rolled into March, no complaint)
date('Y-m-d', strtotime('5/6/26')); // -> '2026-05-06' (a century and an order, both guessed)
strtotime('31/02/2026'); // -> false (refused at last — and date prints false as 1970)
CkDate::ToCarbonOrNull('2026-02-31'); // -> null
CkDate::Explain('2026-02-31'); // -> 'not a calendar date'
CkDate::Explain('5/6/26'); // -> 'a two-digit year is ambiguous'
CkDate::Iso8601('6/5/2026'); // -> '2026-06-05T00:00:00Z' (month first, the default)
CkDate::Iso8601(
input: '6/5/2026',
order: DateOrder::DayFirst
); // -> '2026-05-06T00:00:00Z' (you said day first)
CkDate reads strictly and explicitly, never a guess: "tomorrow" is
not a date, a two-digit year is rejected rather than completed, and an all-numeric date
is read in the DateOrder you declare — month first by default,
DayFirst per call for a European source — and refused when the digits do not fit it.
json_decode hands you floats
The cast you never wrote is the one json_decode makes for you. A JSON number is a PHP
float by the time you see it, whatever it looked like on the wire — about fifteen
significant digits, and INF beyond what a float holds. An identifier of twenty digits
loses its last ones, and an amount is a float from then on:
$raw = json_decode('{ "id": 12345678901234567890, "total": 19.99, "big": 1e400 }', true);
$raw['id']; // -> 1.2345678901234567E+19 (a float: the last digits are gone)
is_infinite($raw['big']); // -> true (INF, which no column holds)
0.1 + 0.2 === 0.3; // -> false (what float arithmetic is like)
Send an identifier that may not fit a 64-bit integer, and every amount, as a JSON
string — "id": "12345678901234567890" — and read the bag through
CkPayload::FromJson, which keeps a number too large for
a float as its text instead of INF. An amount handed to CkMoney
comes back as text with the currency's decimals, exact, so nothing invites a * or a +:
use SafeCoder\CommonKit\CkDictionary;
use SafeCoder\CommonKit\CkMoney;
use SafeCoder\CommonKit\CkPayload;
$payload = CkPayload::FromJson('{ "id": "12345678901234567890", "total": "19.99", "big": 1e400 }');
CkDictionary::String(
dictionary: $payload,
key: 'id'
); // -> '12345678901234567890' (every digit, as sent)
CkMoney::Multiply(
input: CkDictionary::Value(dictionary: $payload, key: 'total'),
factor: 3
); // -> '59.97' (exact, and text)
CkDictionary::Value(
dictionary: $payload,
key: 'big'
); // -> '1e400' (its text, never INF)
sprintf, number_format and date() write what the machine says
Writing a value out has the same trap in reverse: the machine decides, not you. A number
written with sprintf('%f') or printf obeys the server's locale — its language and
formatting settings — and number_format() groups the thousands with a comma unless told
otherwise. Deploy to a server set to German and your decimal points silently become
commas:
use SafeCoder\CommonKit\CkMoney;
use SafeCoder\CommonKit\CkText;
setlocale(LC_ALL, 'de_DE.UTF-8'); // pretend: a German server
sprintf('%.2f', 1234.5); // -> '1234,50' (a comma, corrupt in JSON)
number_format(1234.5, 2); // -> '1,234.50' (grouped, not a number to any parser)
CkText::NumberToString(1234.5); // -> '1234.5' (the same on every machine)
CkMoney::Format(
input: 1234.5,
currency: 'USD'
); // -> '1234.50'
A date is worse, because date(), mktime() and a DateTime made from text with no zone
all stand on date.timezone — whatever the server's php.ini says — so the same code
writes a different day on a different machine:
use SafeCoder\CommonKit\CkDate;
date_default_timezone_set('America/Chicago'); // pretend: a server in Chicago
date('Y-m-d H:i', 1767225600); // -> '2025-12-31 18:00' (the server's clock, not the moment)
(new DateTime('2026-01-01'))->format('c'); // -> '2026-01-01T00:00:00-06:00' (midnight where the server stands)
CkDate::Iso8601(1767225600); // -> '2026-01-01T00:00:00Z'
CkDate::Iso8601('2026-01-01'); // -> '2026-01-01T00:00:00Z' (the digits as written, in UTC)
Anything leaving your process — payloads, CSVs, log lines you plan to parse — goes
through CkText::NumberToString,
CkMoney::Format for an amount and
CkDate::Iso8601 for a moment, none of which consults the
machine. (echo and (string) on a float stopped listening to the locale in PHP 8;
printf did not.)
A $_POST value can be an array
The last cast is one PHP makes before your code runs at all. The client decides the shape
of $_POST, not your form: a request that says email[]=… turns the entry you expected as
text into an array, and every string function you then call throws a TypeError — in
production, a 500 for a request a bot wrote. The readers refuse the value with a reason
instead, and read the list where a list is allowed:
use SafeCoder\CommonKit\CkDictionary;
use SafeCoder\CommonKit\CkEmail;
use SafeCoder\CommonKit\CkPayload;
$form = ['email' => ['mary@example.com', 'eve@example.com']]; // what $_POST holds for email[]=…&email[]=…
trim($form['email']); // throws TypeError
CkDictionary::String(
dictionary: $form,
key: 'email'
); // -> '' (a list is not one value)
CkEmail::Explain($form['email']); // -> 'a Array is not an email address'
CkDictionary::Strings(
dictionary: $form,
key: 'email'
); // -> ['mary@example.com', 'eve@example.com'] (asked for the list)
CkPayload::FromForm('email[]=mary@example.com'); // -> ['email[]' => 'mary@example.com'] (the key as sent, one value)
CkPayload::FromForm and
FromQuery build no arrays from [] at all — a key is
literal, and when it repeats the last value wins, their declared rule — and, with nothing
passed, they read the raw query string and the raw body themselves, as
CkRequest does, so a bag read off the wire holds what the wire said. Read every field
through CkDictionary, whatever shape the bag came in, and the
shape of one entry can never take the request down.
Where next
- How it works — the five rules, and which reader to call.
- Your first payload — a fifteen-minute tutorial: a messy order payload to clean, typed values, and the whole handler at the end.
- Common mistakes — the traps everyone hits once with the library itself.