What are the advantages of using existing formats like JSON or XML for storing data over creating a custom file format in PHP?

Using existing formats like JSON or XML for storing data in PHP has several advantages over creating a custom file format. These formats are widely supported, making it easier to work with data across different platforms and languages. Additionally, they have built-in parsers and libraries in PHP that simplify the process of reading and writing data. Finally, JSON and XML are human-readable, making it easier to debug and troubleshoot data-related issues.

// Example of storing data in JSON format
$data = [
    'name' => 'John Doe',
    'age' => 30,
    'email' => 'johndoe@example.com'
];

$jsonData = json_encode($data);
file_put_contents('data.json', $jsonData);

// Example of reading data from JSON format
$jsonData = file_get_contents('data.json');
$data = json_decode($jsonData, true);

echo $data['name']; // Output: John Doe