$array = array('firstname' => 'sajad', 'age'=> 28 , 'from' => 'tehran');
$jsontoencode = json_encode($array);
echo $jsontoencode ;
echo "<hr>";
function is_json($jsontoencode)
{
json_decode($jsontoencode);
return json_last_error() === JSON_ERROR_NONE;
}
$jsontoencode = '{"firstname":"sajad","age":28,"from":"tehran"}';
if (is_json($jsontoencode)) {
echo 'JSON is valid';
} else {
echo 'JSON is not valid';
}