Reputation: 245
Question 1: What is the best way to write multiple variables?
I'm currently writing them like this.
fwrite($fp, $var1);
fwrite($fp, $var2);
fwrite($fp, $var3);
...etc
And that makes one long healthy string of output.
Question 2: How do I add line breaks and comments without writing something like:
fwrite($fp, 'var1:');
fwrite($fp, &var1);
<br>?
Upvotes: 2
Views: 309
Reputation: 644
Another option might be to look in to using Monolog (https://github.com/Seldaek/monolog). Add something like the following at the start of your script:
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// create a log channel
$log = new Logger('name');
$log->pushHandler(new StreamHandler('path/to/your.log', Logger::DEBUG));
Then whenever you want to output something add:
// add records to the log
$log->addDebug($var1);
$log->addDebug($var2);
$log->addDebug($var3);
It's a bit more work initially but you end up with a much more flexible solution in the long run.
Upvotes: 0
Reputation: 10202
What the best solution would be depends on the use-case. If you could be a bit more clear about what kind of data you are writing we could give you an advise. But foremost take a look at all different file system functions. There's probably one you like :)
A few options:
Write away an array
$data = array('value 1', 'value 2', 'value 3');
file_put_contents(realpath('path/to/file'), $data);
Or an indexed array
$data = array('var 1' => 'value 1', 'var 2' => 'value 2', ...);
foreach($data as $var => $value) {
fwrite($fp, $var .': '. $value);
}
Or if you want to include newlines (\n
):
$data = array('value 1', 'value 2', 'value 3');
fwrite($fp, implode("\n", $data));
options are numerous... more details = better assistance ;)
Upvotes: 2
Reputation: 2647
Concatenation! You can basically combine a bunch of variables/strings into one big string, and print it all in a single fwrite.
ie:
fwrite($fp, "var1: " . $var1 . "\r\nvar2: " . $var2 );
etc
Upvotes: 3