Schedule A Consultation

    Fields marked * are mandatory

      CA INQUIRY

      Latest php version (7.3) New Features

      This entry was posted on Friday November 22, 2019

      New Features

      JSON_THROW_ON_ERROR

      Not having a sufficient method to deal with mistakes when utilizing JSON has been an issue for a significant long time, and web designers everywhere throughout the world have considered this to be an enormous drawback of the language, 

      The RFC of PHP 7.3 has acknowledged this update by a 23-0 vote, which shows how much this component has been mentioned. 

      Until PHP v7.2, we expected to utilize a workaround to get a mistake from JSON and it was neither solid nor capable at its particular employment.

      Here is an example:

      json_decode(“{“);

      json_last_error() === JSON_ERROR_NONE // the result is false

      json_last_error_msg() // The result is “Syntax error”

      Indeed, we can determinate if our JSON had a blunder, at the same time, this is unmistakably not the best technique to do as such. 

       

      The new banner I am going to show you is a superb option since it offers software engineers the chance to utilize the intensity of special cases that can be overseen inside the “attempt get” square of code. 

       

      How about we see a down to earth model, will we?

      use JsonException;

      try {

          $json = json_encode(“{“, JSON_THROW_ON_ERROR);

          return base64_encode($json);

      } catch (JsonException $e) {

          throw new EncryptException(‘Could not encrypt the data.’, 0, $e);

      }

      As you can see from the previous code block, the json_encode function now has an optional parameter JSON_THROW_ON_ERROR which will catch the error and display it using the following exception methods:

      $e->getMessage(); // like json_last_error_msg()

      $e->getCode(); // like json_last_error()

      The default adaptation of PHP 7.3 toward your existing code will be neutral and since it is an optional parameter after you update your PHP everything will still work as aspected.

      This is one of the most important features of the update, so if you want to dig in and learn more have a look at the official RFC for JSON_THROW_ON_ERROR

       

      Read More….