0

I want to put content from 2 or more requests into a file that will be formatted as:

customer | invoice

Below I managed to get both request to be posted, but how do I get them to be posted in the below format:

Customer1 | Invoice1  
Customer2 | Invoice2    ..etc

PHP Code :

file_put_contents('/root/service_Logs/service.log',"
                             {$this->request->customer}\n",FILE_APPEND); 
file_put_contents('/root/service_Logs/service.log',
                             "{$this->‌​request->invoice}\n‌​",FILE_APPEND);

Edit: Still not apple to get them in that format. Any ideas?

Libby1889
  • 13
  • 2

1 Answers1

0

One way to go about it:

$file = '/root/service_Logs/service.log';
$text = sprintf('%s | %s', $this->request->customer, $this->request->invoice);
file_put_contents($file, $text . PHP_EOL, FILE_APPEND);

I'm using sprintf because it allows me to specify the format of a string separately from the data to that format.

You could also use interpolation, but that's more cluttered in my opinion:

$text = "{$this->request->customer} | {$this->request->invoice}";

But, whatever you do, strive to keep your variables declaration separate from the logic of using those variables. When you wrapped everything up into a single call, it gets lengthy and messy and hard to maintain. Even for throw away lines like logging statements, it's a bad habit to have.

bishop
  • 37,830
  • 11
  • 104
  • 139
  • 1
    I am trying to add a time stamp for each entry . I used the below code: $text = sprintf( date('Y-m-d H:i:s') . " - " '%s | %s', $this->request->customer, $this->request->invoice); but no luck with the output. what did i do wrong? – Libby1889 Feb 07 '17 at 17:41
  • `$text = sprintf('%s %s | %s', date(...), ...);` – bishop Feb 07 '17 at 18:04