Skip to content

Instantly share code, notes, and snippets.

@houssemz
Last active August 20, 2018 15:53
Show Gist options
  • Save houssemz/d243e80fe3a9b65995d233172a97fda0 to your computer and use it in GitHub Desktop.
Save houssemz/d243e80fe3a9b65995d233172a97fda0 to your computer and use it in GitHub Desktop.
Json, AJAX and PHP

JSON and PHP

  • Json is an acronym for JavaScript Object Notation
  • DATA-interchange format
  • 🔥 extension load in PHP by default

PREDEFINED CONSTANTS (examples)

  • JSON_ERROR_NONE confirms whether error occurred or not
  • JSON_ERROR_SYNTAX indicates syntax error
  • JSON_ERROR_UTF8 encoding issues
  • JSON_FORCE_OBJECT outputs an object rather than an array when a non-associative array is used. Especially useful when the recipient of the output is expecting an object and the array is empty

JSON FUNCTIONS :

  • json_decode takes a JSON encoded string and converts it into a PHP variable.
    • json_decode returns the values encoded in json in appropriate PHP type. Values, false and null are returned as true, false, and null respectively.
    • Null is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.
<?php
  $json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
  var_dump(json_decode($json)); // stdClass ...
  var_dump(json_decode($json, true)); // associative array
<?php
  $json = '{"foo-bar": 12345}';

 $obj = json_decode($json);
 // can't access like this $obj->'foo-bar!
 print $obj->{'foo-bar'}; // 12345
  • ❗ common mistakes using json_decode
<?php
  /**
  * The following strings are a valid JAVASCRIPT objects BUT
  * the name and value must be enclosed in double quotes
  */
  $bad_json = "{'bar': 'baz'}";
  json_decode($bad_json); // null

  $bad_json = "{bar: 'baz'}";
  // the name must be enclosed in double quotes
  json_decode($bad_json); // null

  $bad_json = '{"bar": "baz", }';
  // trailing commas are not allowed
  json_decode($bad_json); // null
<?php
  $arr = [
       'months' => [
           'english' => [
               'one',
               'january'
           ]
       ]
   ];

  $json = json_encode($arr);
  json_encode($json, true, 2); // NULL
  json_encode($json, true, 3); // NULL
  json_encode($json, true, 3); // the depth 3 is true; depth should = (number of arrays +1)
<?php
  $json = '{"number": 12345678901234567890}';

  /**
  *  object(stdClass)#1 (1) {
  *     ["number"]=>
  *     float(1.2345678901235E+19)
  *   }
  */
  var_dump(json_decode($json));

 /**
 * object(stdClass)#1 (1) {
 * ["number"]=>
 * string(20) "12345678901234567890"
 * }
 */
  var_dump(json_decode($json, false, 512, JSON_BIGINT_AS_STRING));
  • ⚠️ The JSON spec is not JavaScript, but a subset of JavaScript.

  • ⚠️ If the event of a failure to decode, json_last_error() can be used to determine the exact nature of the error.

  • ⚠️ Tips :

<?php
 $json = '{"": "123"}';
 $obj = json_decode($json);
 var_dump($obj->{""}); // string(3) "123"
<?php
  var_dump(json_encode('Hello')); // string(7) ""Hello"" <= double double cotes!

  var_dump(json_decode('Hello')); // null
  var_dump(json_decode("Hello")); // null
  var_dump(json_decode("'Hello'")); // null

  var_dump(json_decode('"Hello"')); // string(5) "Hello"
  • json_last_error returns the last error occurred (an integer).
  • json_last_error_msg returns the error string of the json_last_error().
<?php
 json_decode('Hello');  // wrong
 echo json_last_error(), " : ", json_last_error_msg(); // 4 : Syntax error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment