1

My code in php and jquery

<script type="text/javascript">
$(function() {
    var city_list = <?php echo json_encode($_REQUEST['cities']); ?>
    alert("city_list:: "+city_list);
});

This is working fine in my local where my php version is 5.3.1, but when i put it is server where my php version is 4.3.11 is not working...

I need it to work on both local and sever side.

ljubiccica
  • 480
  • 3
  • 17
user1986299
  • 2,993
  • 2
  • 13
  • 7
  • 2
    [Look at the documentation](http://us2.php.net/manual/en/function.json-encode.php)... It doesn't exist before PHP 5.2. – Nightfirecat May 13 '13 at 06:38
  • 1
    Seriously, you still have a server running PHP v4.3???? Wow. Just wow. I hope it's not running anything important. – Spudley May 13 '13 at 06:50
  • I wouldn't call that "behaves *differently*"... Many, *many* things behave "differently" between PHP 4.3 and 5.3, there are more than 6 years of development history in between. – deceze May 13 '13 at 06:52
  • If you really *really* need to encode json in older php versions, there's a discussion of various options here: http://stackoverflow.com/questions/5618925/convert-php-array-to-javascript/5619038. (but if you've got any sense at all, you'll be thinking about how you can upgrade to a recent php version instead) – Spudley May 13 '13 at 06:54

2 Answers2

4

Here is a file you can use for PHP < 5.2.

http://www.boutell.com/scripts/jsonwrapper.html

And you can compare versions like this:

if (version_compare(PHP_VERSION, '5.2.0', '<')) {
    //include or require jsonwrapper file
}
ljubiccica
  • 480
  • 3
  • 17
1

According to the documentation, json_encode was introduced in php 5.2.0. All is not completely lost, however, as you will find an implementation in the comments there for converting arrays to json. I can't vouch for it, but it ought to at least give you a start.

Jerry
  • 3,391
  • 1
  • 19
  • 28