How can one properly format a JSON string in PHP to avoid syntax errors?

When creating a JSON string in PHP, it is important to properly escape special characters to avoid syntax errors. One way to do this is by using the `json_encode()` function, which will handle the formatting and escaping of the string for you. This function will ensure that the JSON string is correctly formatted and can be parsed without any issues.

$data = array(
    'name' => 'John Doe',
    'age' => 30,
    'city' => 'New York'
);

$jsonString = json_encode($data);
echo $jsonString;