php response json example
Example 1: php change header to json
<?PHP
$data = ;
header('Content-Type: application/json');
echo json_encode($data);
Example 2: json php
$person = array(
"name" => "KINGASV",
"title" => "CTO"
);
$personJSON=json_encode($person);
$personJSON = '{"name":"KINGASV","title":"CTO"}';
$person = json_decode($personJSON);
echo $person->name;
Example 3: php return json
header('Content-type: application/json');
echo json_encode($array);
Example 4: php return json data
header('Content-Type: application/json');
$colors = array("red","blue","green");
echo json_encode($colors);
Example 5: web api return json example in php
$option = $_GET['option'];
if ( $option == 1 ) {
$data = [ 'a', 'b', 'c' ];
} else {
$data = [ 'name' => 'God', 'age' => -1 ];
}
header('Content-type: application/json');
echo json_encode( $data );