JSON Return from PHP to JavaScript
To return JSON data from PHP to JavaScript, utilize the built-in PHP function json_encode(). Here's how you can employ it:
$result = [ ['line1', 'line2'], ['line1', 'line2'], ]; $json = json_encode($result);
$json will now contain a JSON-formatted string that can be easily parsed by JavaScript.
Instead of manually constructing JSON strings, it's recommended to leverage json_encode() as it provides error-handling and ensures data integrity.
The above is the detailed content of How Can I Return JSON Data from PHP to JavaScript?. For more information, please follow other related articles on the PHP Chinese website!