How can JSON be properly written to a file using PHP?

To properly write JSON to a file using PHP, you can follow these steps: 1. Create an array or object with the data you want to encode as JSON. 2. Use the `json_encode()` function to convert the array or object into a JSON string. 3. Use the `file_put_contents()` function to write the JSON string to a file.

$data = array("name" => "John Doe", "age" => 30);
$jsonString = json_encode($data);
file_put_contents('data.json', $jsonString);