amitmerchant1990 / amitmerchant-dot-com-comments

1 stars 0 forks source link

return-javascript-code-from-php/ #111

Open utterances-bot opened 9 months ago

utterances-bot commented 9 months ago

How to return multi-line JavaScript code from PHP — Amit Merchant — A blog on PHP, JavaScript, and more

Sometimes, you may wish to return multi-line JavaScript code from PHP along with some variables. For instance, a JavaScript object.

https://www.amitmerchant.com/return-javascript-code-from-php/

chrispian commented 9 months ago

If you are just echoing a json object I wouldn't do that manually. Here's how I would do this:

$data = [
    'name' => 'John Doe',
    'age'  => 10,
];

header('Content-Type: application/json; charset=utf-8');
echo json_encode($data);