Kay
Kay

Reputation: 87

JSON not working in php script

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?

Upvotes: 0

Views: 2182

Answers (2)

Daniel W.
Daniel W.

Reputation: 32270

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

Upvotes: 0

John Himmelman
John Himmelman

Reputation: 22000

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.

Upvotes: 1

Related Questions