Answers for "php html is breaking json output"

PHP
3

php echo json

<?php
$data = ['name' => 'John', 'age' => 35];
header('Content-type: Application/json');
echo json_encode($data);
Posted by: Guest on September-25-2020
2

php return json

// function to return JSON response, you can set cors, also give status & data via arguments
function json($status, $data) {
        $cors = "*";
        header("Access-Control-Allow-Origin: $cors");
        header('Content-Type: application/json; charset=utf-8');
        http_response_code($status);
        echo json_encode($data);
    }

// example
json(200, [
	"status" => 200,
  	"message" => "action completed successfully",
  	"id" => 44236
]);
Posted by: Guest on October-04-2021

Browse Popular Code Answers by Language