w3resource

PHP JSON Exercises : Decode larger integers

PHP JSON: Exercise-2 with Solution

Write a PHP script to decode large integers.

Sample integer :
{"number": 123456789012345678901234567890}

Sample Solution:

PHP Code:

<?php
// Define a JSON string containing a large number as a string value
$json = '{"number": 123456789012345678901234567890}';

// Decode the JSON string into a PHP object
var_dump(json_decode($json));
?>

Output:

object(stdClass)#1 (1) {                                            
  ["number"]=>                                                      
  float(1.2345678901235E+29)                                        
}

Explanation:

In the exercise above,

  • $json = ...: Define a JSON string containing a large number as a string value.
  • json_decode($json): Decode the JSON string into a PHP object and return the result.
  • var_dump(...): Output the result of "json_decode()" for debugging purposes.

Flowchart :

Flowchart: Decode larger integers

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a PHP script to decode a JSON string.
Next: Write a PHP script to get JSON representation of a value from an array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/php-exercises/php-json-exercise-2.php