PHP include files are making JSON data invalid -


i'm relatively new php development not web development in general.

i have following php file:

<?php class dialogresult{      var $message;     var $title;     var $height;     var $genericdata;       function __construct(){         $this->height = 10;     } }     header("cache-control: no-cache", true);    header("content-type: application/json; charset=utf-8", true);    $dr = new dialogresult();    $dr->message = "a test message encoded";    $dr->height = 10;    $dr->genericdata = "empty";    $dr->title = "my message";    echo(json_encode($dr)); ?> 

this returns json data expected, if move class separate file , add include, include_once, require, or require_once returns invalid json data. can tell me why be?

it doesn't have moving class, if have included file makes data invalid.

thanks,

keith

here include class, i've tried removing ?>

<?php  class dialogresult{      var $message;     var $title;     var $height;     var $genericdata;       function __construct(){         $this->height = 10;     } } 

there no leading or trailing spaces anywhere. here 'invalid' json returned:

{"message":"a test message encoded","title":"my message","height":10,"genericdata":"empty"} 

which gives me "unexpected token" if try use jsonlint parse it. of course, if type in hand jsonlint fine. imagine there character can't identify/see showing in json not sure how find it.

even martin , navnav comments right, whitespaces don't invalid json format can binary formats (eg. gif, jpeg, etc.).

so problema can bom header 2/3 byte header many editors don't show or, worse, add, @ beginning of file.

edit

a possible way remove bom use , ide phpstorm, having binary safe editor, able detect , remove bom.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -