What is the purpose of using json_encode() in PHP?

The purpose of using json_encode() in PHP is to convert a PHP array or object into a JSON string. This is useful when you need to pass data from PHP to JavaScript or other languages that support JSON. It allows for easy serialization and deserialization of data.

// Sample PHP code using json_encode()
$data = array("name" => "John Doe", "age" => 30, "city" => "New York");
$json_data = json_encode($data);

echo $json_data;