Throws Exception with line number on syntax or structural errors.
toml_stringify(array $data): string Serializes an associative array into formatted TOML.
Emits top-level scalar/array keys first, followed by [table] sections and [[array_of_tables]] sections.
Quotes keys and escapes strings automatically.
Throws Exception if the root array is a list or contains unsupported values (e.g., null).
Class TomlDoc
TomlDoc::__construct(array $data = []) Creates a TomlDoc wrapping the given associative array.
TomlDoc::parse(string $input): TomlDoc Parses a TOML string and returns a TomlDoc instance.
$doc->get(string $path, mixed $default = null): mixed Retrieves a value using a dot-separated path (e.g. "database.pool.max"), returning $default if any segment is missing.
$doc->has(string $path): bool Returns true if the dot-separated path exists in the document.
$doc->set(string $path, mixed $value): void Sets a value at the given dot-separated path, creating intermediate tables as needed. Throws Exception if $path is empty.
$doc->toArray(): array Returns the underlying associative array.
$doc->toString(): string Serializes the document back to a TOML string via toml_stringify().
Error Handling
All parse and serialization errors throw Exception with a clear toml: prefix (and line N: for parser errors):
toml: line 2: unterminated basic string
toml: line 2: expected '=' after key
toml: line 2: expected ']' to close table header
toml: line 2: duplicate key 'a'
toml: line 2: unterminated array
toml: root value must be a table (associative array), not a list
toml: TOML does not support null values
Examples
01Quick start
Bundled with the compiler — import "toml"; works offline in any file, or add it to a project with hphp install toml.