What are some alternative data formats, such as XML or CSV, that can be used instead of writing to text files in PHP?

When writing data to files in PHP, instead of using text files, alternative data formats like JSON, XML, or CSV can be used. These formats offer structured data storage, making it easier to read and manipulate the data later on. By using these formats, it also allows for better interoperability with other systems that may expect data in a specific format.

// Example of writing data to a JSON file instead of a text file
$data = ['name' => 'John Doe', 'age' => 30, 'email' => 'john.doe@example.com'];
$jsonData = json_encode($data);
file_put_contents('data.json', $jsonData);