Read the latest web development and design tips at Fred Wu's new blog! :-)
Poke me on GitHub

json_encode() for PHP4 and early PHP5

Had a beauty today. There I was ready to deploy some nicely polished code to the server. After some thorough local testing it looked like it’d be a smooth process. So the code went up, but all my funky AJAX stuff stopped working on the server. How could that be? It was perfect locally… :)

It took a little while but in the end I realised what was going on – json_encode wasn’t working. The server was running PHP 5.1.6 and json_encode only became “standard” with PHP 5.2.0 onward.

I needed a solution fast. No time to recompile a newer version of PHP, add libraries or anything fancy like that. I just needed the function json_encode to work right now. Thankfully, the solution was as easy as adding replacement a function a user kindly submitted from the PHP site itself:

I blindly assumed PHP 5 was PHP 5. I wasn’t using any extremely fancy commands or anything, but I still came unstuck. So the moral of today is check your server specification right down to every last decimal point! ;)

  • Digg
  • DZone
  • Facebook
  • Mixx
  • Google Bookmarks
  • LinkedIn
  • Live
  • Reddit
  • Slashdot
  • StumbleUpon
  • Technorati
  • Twitter

Related posts

Tags: , , , , ,

Comments Section

8 Responses to “json_encode() for PHP4 and early PHP5”

Sidebar might be covered by comments ... consider it a feature! ;)
  1. 1

    thanks for the link, i was having the same problem.

  2. 2

    Thanks for the link. I got spoiled using json_encode and then had to go back to another project that has to work on older versions of PHP.

  3. 3

    Thanks – me too:) Weird thing is that it gives you a nice 200 OK response with nothing in it.

  4. 4

    It would have been better if you had copied that source code on your blog (but still leave the URL to where you find it – for purposes of giving credit where credit is due).

    I want to that link and was not able to locate item/post #82904 making this post moot.

  5. 6


  6. 7

    Thanks for the Link. it solved my problem

  7. 8

    THANKS A LOT! I was making me crazy. Works perfectly

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>