2

I'm having a problem where on a server the output of var_dump and print_r come out entirely garbled. print_r outputs pure gibberish (eg. ��]{W�8�����- ... etc), while var_dump at least gives string (1664), followed by similar gibberish (though this time wrapped in double quotes).

This looks like a character encoding issue, but no encoding I can find seems to fix it (and I don't know why just dumping a PHP object should be outputting non-ascii characters anyway), and echo works fine. Alternatively, I wonder if it could be a gzip issue. Either way, I suspect it must be something in PHP or Apache's configuration, but I have no idea how to fix it.

I'd be very grateful if anyone has any suggestions as to how fix this!


Update: on further investigation, it seems it's a problem specific to the particular object I'm trying to dump. The object in question is decoded JSON requested (via curl) from an API. Is it possible that either json_decode or curl could be misconfigured / mangling the encoding?

Nick F
  • 9,781
  • 7
  • 75
  • 90
  • Possible duplicate of : http://stackoverflow.com/questions/4279282/set-http-header-to-utf-8-using-php – Alex Jan 16 '15 at 17:22
  • No, as far as I can see, it's a different issue. This is not a general encoding problem. It's specifically a problem with the output of `print_r` and `var_dump` – Nick F Jan 16 '15 at 17:23
  • What exactly are you trying to print ? – Alex Jan 16 '15 at 17:24
  • I'm trying to dump a PHP object representing an API response. Interestingly, I can dump strings and arrays ok, it seems, but this particular object gets totally garbled. – Nick F Jan 16 '15 at 17:28
  • This definitely isn't answered by the "possible duplicate" question - as stated in the question, (1) I've tried different encodings, and (2) if it were a general HTML / HTTP encoding problem, it doesn't seem likely that it would only affect the output of `var_dump` / `print_r` – Nick F Jan 16 '15 at 17:32
  • You trying to parse a json ? You maybe show some code explaining what you are trying to do, because there are different types of encoding issues. – Alex Jan 16 '15 at 17:33
  • Try to use `$contents = utf8_encode($contents);` than `$results = json_decode($contents); ` – Alex Jan 16 '15 at 17:54

1 Answers1

0

For what it's worth, I finally got to the bottom of this problem (I think!)

The problem seems to be that the API's output was being run through json_decode whether it was JSON or not. MySQL errors were causing an error page, not a JSON response, which when run through json_decode (by the API-handling code that received it) before var_dump produced garbled character salad, as above.

Nick F
  • 9,781
  • 7
  • 75
  • 90