How can PHP be used to convert and display data from one format to another based on user input?

To convert and display data from one format to another based on user input in PHP, you can use conditional statements to determine the user's choice and then apply the appropriate conversion function. For example, if the user selects to convert data from JSON to XML, you can use PHP's json_decode() function followed by xmlrpc_encode() to achieve this conversion.

$user_input = $_POST['conversion_type'];
$data_to_convert = $_POST['data'];

if ($user_input == 'json_to_xml') {
    $json_data = json_decode($data_to_convert);
    $xml_data = xmlrpc_encode($json_data);
    echo $xml_data;
} elseif ($user_input == 'xml_to_json') {
    $xml_data = simplexml_load_string($data_to_convert);
    $json_data = json_encode($xml_data);
    echo $json_data;
} else {
    echo "Invalid conversion type selected.";
}