<?php
function safeJsonDecode(string $json, bool $assoc = true, int $depth = 64): array|object|null|false {
if ($json === '') return false;
try {
return json_decode($json, $assoc, $depth, JSON_THROW_ON_ERROR);
} catch (JsonException $e) {
error_log('JSON decode failed: ' . $e->getMessage());
return false;
}
}
$data = safeJsonDecode('{"ok": true}'); // ['ok' => true]
$bad = safeJsonDecode('{not json}'); // false
if ($bad === false) { /* handle parse error */ }
Create a free account and build your private vault. Share publicly whenever you want.