开发者

outputting all array values to file with php

开发者_开发技巧Im working with a foreign API and im trying to get a fix on what all its sending to my file with the GET method.

How can i output something like print_r($_GET) to a file so i can read what all its sending?


If you have a hash, both of the listen solutions won't give you the keys. So here's a way to get your output formatted by print_r:

$var = print_r($your_array, 1);
file_put_contents("your_file.txt",$var);

The second option of print_r is boolean, and when set to true, captures the output.


It sounds like you need a log to store the $_GET variables being submitted to your script, for debug purposes. I'd do something like this appending values to the end of the file, so the file is not overwritten every request:

file_put_contents('path_to_log.txt', print_r($_GET, true), FILE_APPEND);


Writing to a file:

You could use file_put_contents() to create the file with your output. This function will accept a string, or an array, which releases you of the burden to convert your array into a writable-format.

file_put_contents("myget.txt", $_GET);

As stated in the comments, this option isn't ideal for multidimensional arrays. However, the next solution is.

Maintaining format:

If you'd like to maintain the print_r() formatting, simply set the second parameter to true to return the value into a variable rather than outputting it immediately:

$output = print_r($_GET, true);
file_put_contents("myget.txt", $output);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜