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?

16
  • 6
    What exactly is happening when you say "nothing works"? Are you getting an error, or no output? Commented Aug 12, 2013 at 19:45
  • 1
    Where is this PHP block in your page ? Is that the whole page ? Commented Aug 12, 2013 at 19:46
  • 2
    Try using json_last_error() to see what error you're getting. php.net/manual/en/function.json-last-error.php Commented Aug 12, 2013 at 19:46
  • @StephenTG no output. Commented Aug 12, 2013 at 19:46
  • 2
    Add ini_set('display_errors', 1); error_reporting(-1); to the top of your file. Commented Aug 12, 2013 at 19:48

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

text/pain. That made me laugh :)
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.