file_put_contents 从多个请求到特定格式 PHP

file_put_contents from multiple requests into specific format PHP

我想将来自 2 个或更多请求的内容放入一个格式如下的文件中:

customer | invoice

下面我设法让两个请求都发布了,但是我如何让它们以下面的格式发布:

Customer1 | Invoice1  
Customer2 | Invoice2    ..etc

PHP代码:

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

编辑:仍然不是 apple 以那种格式获取它们。有什么想法吗?

一种解决方法:

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

我正在使用 sprintf 因为它允许我指定字符串的 格式 与数据 分开那种格式。

您也可以使用插值法,但在我看来这更混乱:

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

但是,无论您做什么,都应努力将变量声明与使用这些变量的逻辑分开。当您将所有内容打包到一个调用中时,它会变得冗长、混乱且难以维护。即使是像日志语句这样的丢弃行,也是一个坏习惯。