- var_dump: documentation ( source)
- json_last_error_msg: documentation ( source)
- json_decode: documentation ( source)
- json_last_error: documentation ( source)
<?php
$invalid = '{"test":"foo}';
$valid = '[]';
try {
json_decode($invalid, true); // this fails silently
json_decode($valid, true, 512, JSON_THROW_ON_ERROR);
// Which is what Safe does to check if it needs to throw an exception
if (json_last_error() !== JSON_ERROR_NONE) {
var_dump('With the flag', json_last_error_msg());
}
json_decode($valid, true);
// Which is what Safe does to check if it needs to throw an exception
if (json_last_error() !== JSON_ERROR_NONE) {
var_dump('Without the flag', json_last_error_msg());
}
} catch (Throwable $e) {
echo 'This should not even be triggered: ' . $e->getMessage() . PHP_EOL;
}