php_encode
lib callCapy
function php_encode(value : as any) any
Description
php_encode encodes data without starting PHP. It supports none, booleans, numbers, byte strings, lists, and maps.
Lists use PHP integer keys in numeric order. Maps use PHP string keys in insertion order. PHP can convert canonical numeric string keys to integer keys. Entry order survives a decode and encode cycle, but sparse integer key types can change to strings. Empty maps and empty lists both encode as PHP's empty array.
Signed integers use PHP integer serialization without a floating-point conversion. Unsigned integers use PHP integer serialization when their value fits in the signed 64-bit range. Larger unsigned values return ok: false with error_code: "integer_range".
Finite whole f64 values with magnitude at most 9007199254740991 use PHP integer serialization, except negative zero. Other floating-point values use PHP floating-point serialization. Thus an integer-valued f64 can decode as s64.
Strings remain PHP strings, including strings that contain decimal digits. Byte string lengths count bytes, not Unicode characters.
The codec limits output to 8 MiB and bounds nesting and entries. Objects, callables, opaque handles, and reference graphs are not supported. This function does not frame PHP session data or change Capy session storage.
Parameters
value : the value to serialize
Return Values
{ok: true, value: ...} on success, or {ok: false, error: "...", error_code: "..."} on failure. The value field contains the PHP serialization byte string.
Errors
A failure has no value. error_code identifies the failure and error describes it. Failure does not throw, trap, or stop the request.
Example
Capy
try encoded := php_encode(name: "Ada", active: true)
{
print(encoded, "\n")
}
else
{
print(encoded.error, "\n")
}
Output
a:2:{s:4:"name";s:3:"Ada";s:6:"active";b:1;}