1

我有:

 if ( isset( $_POST["test"] ) ) { 

$html1 = $object->htmlmarkup1();
$html2 = $object->htmlmarkup2();
$json = array("html1" => $html1, "html2" => $html2);

die(json_encode($json));
}

这些函数根据“测试”POST 数据的一些计算来回显 html 标记。这些函数使用 echo 而不是 return,因为我在其他地方使用这些函数,而且我的代码格式更容易回显函数的结果,而不是先返回结果。

我通过将“test1”和“test2”放在两个数组元素中并在我的测试页面中正确地解码并显示“test1”和“test2”,在不使用函数的情况下对此进行了测试。

4

1 回答 1

3

您可以使用输出缓冲。这允许您将输出保存在缓冲区中,而不是将其发送到客户端,然后将其取回(例如,将其存储在变量中)。

请参阅ob_start()ob_get_clean()和所有其他相关函数。

// from now on, output is not sent to the client but saved in a buffer
ob_start(); 
$object->htmlmarkup1();
// get the content of the buffer into $html1 and turn off output buffering
$html1 = ob_get_clean(); 

ob_start();
$object->htmlmarkup2();
$html2 = ob_get_clean();

$json = array("html1" => $html1, "html2" => $html2);
于 2013-06-13T23:25:22.337 回答