What is the role of json_encode in PHP and how does it handle special characters?
json_encode in PHP is used to convert a PHP array or object into a JSON string. When dealing with special characters, json_encode automatically escapes them to ensure they are properly encoded in the JSON output. This ensures that the JSON string is valid and can be safely transmitted or stored.
$data = array(
'name' => 'John Doe',
'description' => 'Special characters like " and \ are properly handled by json_encode.'
);
$json_string = json_encode($data);
echo $json_string;