title: How do I encode an array to JSON without json_encode()?
body: <p>I'm very very new to PHP, but now I have to help my friend to solve a PHP problem.</p>
  
  <p>He has bought a web site based on PHP, after he upload it to the host, found there is a error:</p>
  
  <pre><code>Fatal error:  Call to undefined function json_encode() in xxx.php
  </code></pre>
  
  <p>The code is very simple:</p>
  
  <pre><code>echo json_encode(array(
      'result' =&gt; true,
  ));
  </code></pre>
  
  <p>It send a json to client.</p>
  
  <p>I know <code>json_encode</code> is added after php 5.2, but the PHP version of his host is PHP 5.1.2, so that's the reason why it reports error.</p>
  
  <p>But we don't have the right to upgrade the PHP version of host, so I have to modify the code. How to let it return a json to client without <code>json_encode</code>?</p>
  
  <p>The webpage in client browser will run this javascript code, to get and check that json:</p>
  
  <pre><code>jQuery.ajax({ 'url': '...', ...,
      'success':function(json) {
          if(json != null &amp;&amp; json.result == true) {
             // do something
          }
      }
  }
  </code></pre>
  
  <p>Thanks a lot!</p>
  
  <hr>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Since I have no rights to upgrade anything or install new libraries to that host, the only thing I can do is change the code. But I nearly know nothing about php, that I don't know how to use 3rd party libraries either. At last, I fix it as this:</p>
  
  <p>In php:</p>
  
  <pre><code>echo '{"result":"true"}';
  </code></pre>
  
  <p>In javascript:</p>
  
  <pre><code>if(json!=null &amp;&amp; json.result=="true") {
      // do something
  }
  </code></pre>