PHP JSON Files
JSON (JavaScript Object Notation) is the standard format for data exchange. Perfect for APIs, configuration files, and structured data storage!
Output
Click Run to execute your code
Write JSON
<?php
$data = [
['name' => 'John', 'age' => 30],
['name' => 'Jane', 'age' => 25]
];
$json = json_encode($data, JSON_PRETTY_PRINT);
file_put_contents('users.json', $json);
?>
Read JSON
<?php
$json = file_get_contents('users.json');
$data = json_decode($json, true); // true for array
print_r($data);
echo $data[0]['name']; // John
?>
Summary
- json_encode(): PHP to JSON
- json_decode(): JSON to PHP
- JSON_PRETTY_PRINT: Readable format
- Use case: APIs, config files
What's Next?
Finally, learn about File Permissions - security and access control!
Enjoying these tutorials?