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));
?>

 

 

Link to comment
Share on other sites

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));

?>

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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));

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.