0

I would like to ask a simple question. I have different variables $a='a', $b='b', $c='c' in PHP. I would like to save the target txt file with the format like

"a b c". 

But if I use fwrite, I could only write the variables in a column.

"a
 b
 c"

Anyone could help me? Thanks in advance.

PS

I found problem may be here. Each variable I used is from the lines of a existed file.

 $lines= files('abc.txt'); 
 $a = $lines[0]; //like this

In this circumstance, the variables contains also a line feed ? Am I write ?

user2152814
  • 257
  • 1
  • 7
  • 17

3 Answers3

2

Use a single fwrite statement to write them all at once:

fwrite( $fp, "$a $b $c" );
Nick Coons
  • 3,682
  • 1
  • 19
  • 21
1

Join them as a single string:

$str = $a.' '.$b.' '.$c;

Then write them to the file.

Expedito
  • 7,771
  • 5
  • 30
  • 43
0

I am a little confused about your query but as per what I have understood is that you want something like this:

$a='a';
$b='b';
$c='c';
$fp = fopen('data.txt', 'w');
fwrite($fp,$a." ");
fwrite($fp,$b." ");
fwrite($fp,$c);
fclose($fp);
Andre Silva
  • 4,782
  • 9
  • 52
  • 65
Pranay Bhardwaj
  • 1,059
  • 8
  • 9