PHP’s json_decode behaving badly

Funny thing with PHP’s json_decode…

$ php
<?php echo var_export(json_decode("true ", true), true) ?>
NULL

(Press ctrl-d after you write “?>”.)

The same using node.js:

$ node
> JSON.parse("true ")
true
> exit

Of course it could be argued that it’s a good feature to not ignore leading white spaces.

However most text editors are going to write at least one line break at the end of files — and that kind of line break was the problem of my bug at the moment.

Original available as gist #2353872.

Fork me on GitHub

About jhh

Programmer, business owner and a system admin. I post about books, business, Internet, technology and pretty much anything interesting, too.
This entry was posted in Programming, Web development & design. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Anti-Spam Quiz: