w3resource

PHP JSON Exercises : Decode larger integers


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

Go to:


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

PHP Code Editor:



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

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.