1
2<?php
3// A valid json string
4$json[] = '{"Organization": "PHP Documentation Team"}';
5
6// An invalid json string which will cause an syntax 
7// error, in this case we used ' instead of " for quotation
8$json[] = "{'Organization': 'PHP Documentation Team'}";
9
10
11foreach ($json as $string) {
12    echo 'Decoding: ' . $string;
13    json_decode($string);
14
15    switch (json_last_error()) {
16        case JSON_ERROR_NONE:
17            echo ' - No errors';
18        break;
19        case JSON_ERROR_DEPTH:
20            echo ' - Maximum stack depth exceeded';
21        break;
22        case JSON_ERROR_STATE_MISMATCH:
23            echo ' - Underflow or the modes mismatch';
24        break;
25        case JSON_ERROR_CTRL_CHAR:
26            echo ' - Unexpected control character found';
27        break;
28        case JSON_ERROR_SYNTAX:
29            echo ' - Syntax error, malformed JSON';
30        break;
31        case JSON_ERROR_UTF8:
32            echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
33        break;
34        default:
35            echo ' - Unknown error';
36        break;
37    }
38
39    echo PHP_EOL;
40}
41?>
42
431$person = array( 
2    "name" => "Johny Carson", 
3    "title" => "CTO"
4); 
5$personJSON=json_encode($person);//returns JSON string1
2  <?php
3$age = array("Peter"=>35, "Ben"=>37, "Joe"=>43);
4
5  
6echo json_encode($age);
7?>
8