dongyou5271 2014-07-16 12:21
浏览 37
已采纳

file_put_contents包含多个变量

I'm trying to figure out how to write multiple variables to file using file_put_content.

My code:

<?php
$file = 'file.txt';
$data1 = "John Smith";
file_put_contents($file, $data1, FILE_APPEND | LOCK_EX);
?>

I have additional variables such as $data2, $data3 and $data4. I tried:

<?php
$file = 'file.txt';
$data1 = "John Smith";
$data2 = "Street Address";
$data3 = "Plumber";
$data3 = "Phone nuber";
file_put_contents($file, $data1, $data2, $data3, $data4,FILE_APPEND | LOCK_EX);
?>

but it's not working. I need to write all variables to file.txt so it looks like this:

John Smith Stret Address Plumber Phone Number

and each time when the script has been launched it should add new variables in a new line e.g:

John Smith Stret Address Plumber Phone Number
Mike Johnson Stret Address lectrician Phone Number

etc etc. Any ideas? :)

  • 写回答

2条回答 默认 最新

  • duanqiu2064 2014-07-16 12:32
    关注

    I would do it this way:

    <?php
    $file = 'file.txt';
    
    $data1 = "John Smith";
    $data2 = "Street Address";
    $data3 = "Plumber";
    $data3 = "Phone nuber";
    
    $data = array($data1, $data2, $data3, $data4);
    
    file_put_contents($file, implode(' ',$data)."
    ",FILE_APPEND | LOCK_EX);
    ?>
    

    You can create array and implode it using any separator you want and concatenate implode result with new line .

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题