How can JSON be used to efficiently transfer data from PHP to JavaScript, according to the suggestions in the thread?

When transferring data from PHP to JavaScript, using JSON can be an efficient method. JSON allows for easy serialization and deserialization of data, making it simple to pass complex data structures between the two languages. By encoding PHP data into JSON format before sending it to JavaScript, you can ensure that the data is easily accessible and usable on the client-side.

<?php
$data = array("name" => "John Doe", "age" => 30, "city" => "New York");
$json_data = json_encode($data);
echo "<script>var jsonData = $json_data;</script>";
?>