2

i have a small function to write contents

function write($data)
{
  file_put_contents("filename.txt",$data,FILE_APPEND);
}

it works good except some where when i do the following

write( var_dump($someObject) );

this gives an error message of

illegal character.

I see the this error message through an alert box in browser the php gets call through an ajax call. Any help in debugging this would be appreciated thanks.

4

2 回答 2

3

var_dump不会那样工作,因为它直接输出而不是返回输出。如果要捕获输出以便将其写入其他位置,则必须使用输出缓冲

ob_start();
var_dump($data);
write(ob_get_clean());

或者,var_export只要您记得传递true第二个参数,这也是可行的:

write(var_export($data, true));
于 2012-07-17T09:31:01.640 回答
2

该功能var_dump()请参阅文档)确实会回显所有内容。您应该在此处使用var_export()请参阅文档)。

write(var_export($data, true));
于 2012-07-17T09:27:33.563 回答