Jump to content

Writing output to a file


Moron

Recommended Posts

Can you post your most recent code now.

 

Sure. It is:

 

<?php
$tmp = array();
?>

<?php

$AccountNumber = "00040";


if ($AccountNumber = 00040)    
{
$tmp[] = "<ACCOUNT_NO>";
    	$tmp[] = "Account Number is 00040";
$tmp[] = "</ACCOUNT_NO>\n";
}
else
{
$tmp[] = "<ACCOUNT_NO>";
   	$tmp[] = "Account Number is not 00040";
$tmp[] = "</ACCOUNT_NO>\n";
$tmp[] = '';
}
?>

<?php

$AccountName = "Microsoft";

if ($AccountName = "Microsoft")    
{
$tmp[] = "<ACCOUNT_NAME>";
    	$tmp[] = "Account Name is Microsoft";
$tmp[] = "</ACCOUNT_NAME>\n";
}
else
{
$tmp[] = "<ACCOUNT_NAME>";
   	$tmp[] = "Account Name is not Microsoft";
$tmp[] = "</ACCOUNT_NAME>\n";
}
?>

<?php
file_put_contents('accounts.txt',implode("\n",$tmp));
?>

 

 

OK, I've tried this and it appears to be working:

<?php

// Declare Array
$tmp = array();

// Hard coded details for testing
$AccountNumber = "00040";
$AccountName = "Microsoft";

// Add Account Number
if ($AccountNumber == "00040") {
   $tmp[] = "<ACCOUNT_NO>";
   $tmp[] = "Account Number is 00040";
   $tmp[] = "</ACCOUNT_NO>";
}
else {
   $tmp[] = "<ACCOUNT_NO>";
   $tmp[] = "Account Number is not 00040";
   $tmp[] = "</ACCOUNT_NO>";
}

// Attempt to add a new line to separate sections as implode wont add additional line
$tmp[] = "\n";

// Add Account Name
if ($AccountName == "Microsoft") {
   $tmp[] = "<ACCOUNT_NAME>";
   $tmp[] = "Account Name is Microsoft";
   $tmp[] = "</ACCOUNT_NAME>";
}
else {
   $tmp[] = "<ACCOUNT_NAME>";
   $tmp[] = "Account Name is not Microsoft";
   $tmp[] = "</ACCOUNT_NAME>";
}

// Write contents to a file
file_put_contents('accounts.txt',implode("\n",$tmp));

?>

 

OK, I've tried this and it appears to be working:

<?php

// Declare Array
$tmp = array();

// Hard coded details for testing
$AccountNumber = "00040";
$AccountName = "Microsoft";

// Add Account Number
if ($AccountNumber == "00040") {
   $tmp[] = "<ACCOUNT_NO>";
   $tmp[] = "Account Number is 00040";
   $tmp[] = "</ACCOUNT_NO>";
}
else {
   $tmp[] = "<ACCOUNT_NO>";
   $tmp[] = "Account Number is not 00040";
   $tmp[] = "</ACCOUNT_NO>";
}

// Attempt to add a new line to separate sections as implode wont add additional line
$tmp[] = "\n";

// Add Account Name
if ($AccountName == "Microsoft") {
   $tmp[] = "<ACCOUNT_NAME>";
   $tmp[] = "Account Name is Microsoft";
   $tmp[] = "</ACCOUNT_NAME>";
}
else {
   $tmp[] = "<ACCOUNT_NAME>";
   $tmp[] = "Account Name is not Microsoft";
   $tmp[] = "</ACCOUNT_NAME>";
}

// Write contents to a file
file_put_contents('accounts.txt',implode("\n",$tmp));

?>

 

 

I think it is. It seems that I get a different formatting with Notepad as opposed to Notepad++ as opposed to Wordpad, etc....

 

Thanks! I'll experiment with adding some more elements.

 

If you're viewing and running on Windoze then you might want to try this, it should add the End Of Line character for that specific OS.  On Windoze I believe it's "\r\n"

 

<?php

// Declare Array
$tmp = array();

// Hard coded details for testing
$AccountNumber = "00040";
$AccountName = "Microsoft";

// Add Account Number
if ($AccountNumber == "00040") {
   $tmp[] = "<ACCOUNT_NO>";
   $tmp[] = "Account Number is 00040";
   $tmp[] = "</ACCOUNT_NO>";
}
else {
   $tmp[] = "<ACCOUNT_NO>";
   $tmp[] = "Account Number is not 00040";
   $tmp[] = "</ACCOUNT_NO>";
}

// Attempt to add a new line to separate sections as implode wont add additional line
$tmp[] = PHP_EOL;

// Add Account Name
if ($AccountName == "Microsoft") {
   $tmp[] = "<ACCOUNT_NAME>";
   $tmp[] = "Account Name is Microsoft";
   $tmp[] = "</ACCOUNT_NAME>";
}
else {
   $tmp[] = "<ACCOUNT_NAME>";
   $tmp[] = "Account Name is not Microsoft";
   $tmp[] = "</ACCOUNT_NAME>";
}

// Write contents to a file
file_put_contents('accounts.txt',implode(PHP_EOL, $tmp));

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.