title: PHP: How to encode infinity or NaN numbers to JSON?
body: <p>Apparently, infinity and NaN are not a part of JSON specification, so this PHP code:</p>
  
  <pre><code>$numbers = array();
  $numbers ['positive_infinity'] = +INF;
  $numbers ['negative_infinity'] = -INF;
  $numbers ['not_a_number'] = NAN;
  $array_print = print_r ($numbers, true);
  $array_json = json_encode ($numbers);
  echo "\nprint_r(): $array_print";
  echo "\njson_encode(): $array_json";
  </code></pre>
  
  <p>Produces this:</p>
  
  <pre><code>PHP Warning:  json_encode(): double INF does not conform to the JSON spec, encoded as 0 in /home/septi/test.php on line 8
  PHP Warning:  json_encode(): double -INF does not conform to the JSON spec, encoded as 0 in /home/septi/test.php on line 8
  PHP Warning:  json_encode(): double NAN does not conform to the JSON spec, encoded as 0 in /home/septi/test.php on line 8
  
  print_r(): Array
  (
      [positive_infinity] =&gt; INF
      [negative_infinity] =&gt; -INF
      [not_a_number] =&gt; NAN
  )
  
  json_encode(): {"positive_infinity":0,"negative_infinity":0,"not_a_number":0}
  </code></pre>
  
  <p>Is there any way to correctly encode these numbers without writing my own <code>json_encode()</code> function? Maybe some workaround?</p>