2

I am using phpunit to do some testing and I ran into a problem with

print json_encode($assoc_array);

I have an associative array

$assoc_array("username":"xxx");

In my_function() I print it out like this

print json_encode($assoc_array);

Then in my phpunit test I assert this

$output=my_function($assoc_array);
$expected='{"username":"xxx"}';
$this->assertEquals($expected,$output);

The assert returns false because $output is

$output='
{"username";"xxx"}';

and $expected is

$expected='{"username";"xxx"}';

For some reason I am getting a line break at the beginning when I use print json_encode();

I could just add a line break to all of my $expected values, but I really don't want to do that. Why am I getting the line break? Can anyone else get the line break too?

6
  • echo json_encode($assoc_array); Commented Jan 21, 2014 at 18:45
  • 2
    I don't get a line break. Are you sure there's no other line break earlier in your script? Maybe you have a blank line before <?php? Commented Jan 21, 2014 at 18:45
  • @KrishR What difference does that make? echo and print work the same way. Commented Jan 21, 2014 at 18:45
  • 1
    Try print '|' . json_encode($assoc_array);. Is the line before before or after |? Commented Jan 21, 2014 at 18:46
  • @Barmar There is a line break after the '|' Commented Jan 21, 2014 at 19:29

1 Answer 1

2

Thanks for the help @Barmar. I found a line break after a ?> in a controller. I will remove all the ?> from the class files.

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

1 Comment

I had the same problem! uahuahau, but it's ok now, program many classes has this kind of trouble!

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.