PHP打印json_编码初始换行符

PHP打印json_编码初始换行符,php,printing,json,line-breaks,Php,Printing,Json,Line Breaks,我正在使用phpunit做一些测试,我遇到了一个问题 print json_encode($assoc_array); 我有一个关联数组 $assoc_array("username":"xxx"); 在我的函数()中,我像这样打印出来 print json_encode($assoc_array); 然后在我的phpunit测试中,我断言这一点 $output=my_function($assoc_array); $expected='{"username":"xxx"}'; $this

我正在使用phpunit做一些测试,我遇到了一个问题

print json_encode($assoc_array);
我有一个关联数组

$assoc_array("username":"xxx");
在我的函数()中,我像这样打印出来

print json_encode($assoc_array);
然后在我的phpunit测试中,我断言这一点

$output=my_function($assoc_array);
$expected='{"username":"xxx"}';
$this->assertEquals($expected,$output);
断言返回false,因为$output为

$output='
{"username";"xxx"}';
而预期的美元是

$expected='{"username";"xxx"}';
由于某种原因,当我使用
print json_encode()时,我在开始时得到了一个换行符


我可以在我所有的
$expected
值中添加一个换行符,但我真的不想这样做。为什么我的电话断了?其他人也可以插队吗?

谢谢@Barmar的帮助。我在控制器中的
?>
之后发现一个断线。我将从类文件中删除所有
?>

echo json\u encode($assoc\u数组)我没有换行符。您确定在您的脚本之前没有其他换行符吗?也许你在
@KrishR之前有一个空行,这有什么区别
echo
print
的工作方式相同。请尝试
print'|'。json_编码($assoc_数组)。这行是在
|
之前还是之后?@Barmar在
'|'
之后有一个换行符我也遇到了同样的问题!UAHUAHOU,但现在没事了,很多课程都有这样的问题!