Are there any potential pitfalls or issues with using addslashes() in PHP for creating JSON strings?

One potential issue with using addslashes() in PHP for creating JSON strings is that it may not properly escape all characters, leading to potential security vulnerabilities or malformed JSON. To solve this issue, it is recommended to use the json_encode() function in PHP, which automatically escapes characters and creates valid JSON strings.

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

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