How can PHP scripts be optimized to efficiently handle variable return values without relying on HTML pages as intermediaries?

To efficiently handle variable return values in PHP scripts without relying on HTML pages as intermediaries, you can use JSON encoding to return data in a structured format that can be easily parsed by other scripts or applications. This allows for a more flexible and lightweight way to pass data between different parts of your application.

<?php
// Sample PHP script that returns variable values using JSON encoding

// Sample data to be returned
$data = array(
    'name' => 'John Doe',
    'age' => 30,
    'city' => 'New York'
);

// Encode the data as JSON and output it
header('Content-Type: application/json');
echo json_encode($data);
?>