0

I'm not getting the json_encode to work in my php file. Like for instance, I tried this example I got from php.net

<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

echo json_encode($arr);
?>

But nothing works. If i remove the echo statement, then my php works, which means php isn't recognizing the json_encode code.

I'm using PHP 5.4.16. To sum it up, I'm using xampp 1.8.2.

Help please?

Kay
  • 85
  • 11
  • 6
    What exactly is happening when you say "nothing works"? Are you getting an error, or no output? – StephenTG Aug 12 '13 at 19:45
  • 1
    Where is this PHP block in your page ? Is that the whole page ? – Denys Séguret Aug 12 '13 at 19:46
  • 2
    Try using `json_last_error()` to see what error you're getting. http://www.php.net/manual/en/function.json-last-error.php – Tricky12 Aug 12 '13 at 19:46
  • @StephenTG no output. – Kay Aug 12 '13 at 19:46
  • Works fine for me: http://codepad.org/sq1LmgZb. If you have a problem, you should describe it properly. Especially you have to provide code that *reproduces* the problem. – Felix Kling Aug 12 '13 at 19:47
  • Are you getting anything if you do `print_r($arr)` ? – StephenTG Aug 12 '13 at 19:47
  • @KAy what are you seeing when you remove the json_encode call? – Orangepill Aug 12 '13 at 19:47
  • 2
    Add `ini_set('display_errors', 1); error_reporting(-1);` to the top of your file. – gen_Eric Aug 12 '13 at 19:48
  • @dystroy its at the end. The rest of the code works normally, but just can't use the json code at all. – Kay Aug 12 '13 at 19:48
  • What does it mean, "at the end" ? How is it supposed to be rendered ? – Denys Séguret Aug 12 '13 at 19:50
  • You should check PHP/Apache log files. Maybe something is wrong with your configuration. Like missing some essential element. You would have the answer in the logs. – Thibault Aug 12 '13 at 19:50
  • @Thibault thanks for the suggestion. This is what it says in the log: Can't use function return value in write context – Kay Aug 12 '13 at 19:54
  • Is your script used like this as standalone ? Is it complete ? Is it called by another script like eval or something ? – Thibault Aug 12 '13 at 20:05
  • standalone. I just used that as a test to see if JSON is working. After that statement i have something like echo" alert('works');"; to make sure it works but the message never alerts unless I remove the json_encode statement – Kay Aug 12 '13 at 20:07
  • @Kay: `Can't use function return value in write context`. That means you have `isset(func())` or `empty(func())`; you can't do that. They only work on variables. – gen_Eric Aug 13 '13 at 14:32
  • well it's a multidimensional array i'm trying to send – Kay Aug 13 '13 at 14:37

2 Answers2

1

It sounds like there could be a fatal error somewhere else in your script. Make sure display_errors is set to On in php.ini.

John Himmelman
  • 21,504
  • 22
  • 65
  • 80
0

This question can not be answered without more debug beeing provided.

As in the comments suggested, use these lines before everything to (try to) enable error reporting:

<?php
header('Content-Type: text/plain');
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('display_startup_errors', true);
// Json
echo json_last_error();

Use phpinfo(); to obtain information about PHP Version and JSON support.

This snippet will tell you if the json function is available:

<?php
if (!function_exists('json_encode')) {
    echo 'PHP not compiled with json support', PHP_EOL;
}

Last but not least, check errors logs

/var/log/httpd*
/var/log/nginx*
/var/log/php-fpm*
/var/log/apache*
.... (linux)

From Where does PHP's error log reside in XAMPP?

\xampp\apache\logs\error.log

or

\xampp\php\logs\php_error_log
Community
  • 1
  • 1
Daniel W.
  • 31,164
  • 13
  • 93
  • 151
  • 1
    `text/pain`. That made me laugh :) – gen_Eric Aug 13 '13 at 21:54
  • 1
    Good advice except for the header() function. A) script might have already echoed something to output buffer causing more errors/confusion, B) many configurations use HTML formatted error messages, B) no real point in end – JSON Aug 13 '13 at 22:07