Avatar ·

PHP: How to nicely format JSON (Pretty-Print) for display and file output?

Hello everyone! I'm a beginner in PHP and working on a small project where I need to output data in JSON format to the screen and save it to a log file. Currently I'm using the json_encode() function, but it outputs everything in a single line — this is very hard to read, especially when the data is complex and nested. Here's my current code: ```php [ 'name' => 'Ivan', 'email' => 'ivan@example.com', 'age' => 30, 'hobbies' => ['fishing', 'programming', 'reading'] ], 'status' => 'active', 'last_login' => '2023-10-15 14:30:00' ]; $json = json_encode($data); echo $json; // Save to file file_put_contents('log.json', $json); ?> ``` The output is a mess like: `{"user":{"name":"Ivan","email":"ivan@example.com","age":30