What is the purpose of using json_encode() in PHP when converting data to JSON format?

The purpose of using json_encode() in PHP is to convert data into a JSON format. This function takes an array or object as input and returns a JSON-encoded string that represents the data in a format that can be easily transmitted or stored. This is particularly useful when working with APIs or sending data between different systems that communicate using JSON.

$data = array("name" => "John Doe", "age" => 30);
$json_data = json_encode($data);
echo $json_data;