Steven
Steven

Reputation: 19425

Why isn't \n\n breaking lines when saving text to file?

I have this function which writes content to a text file:

function write($text){
  fwrite($this->fp, '#' . date('H:i d.m.Y') . ': ' . $text . "\n\n");
}

Each time this is called, text is added and new line is invoked.

But if I have do something like this:

$text = 'some text \n\n Some more text.';
write($text)

Then the line break in the text is not "working".

Why is that? What am I missing?

Here is the entire function I'm using to record debugging data:

  class logDebuggData {
    private $fp = NULL;

    function __construct($name='log', $dir=''){
      $this->fp = fopen(TEMPLATE_DIR.$name.'.txt', 'a+');
    }

    function write($text){
      fwrite($this->fp, '#' . date('H:i d.m.Y') . ': ' . $text . "\n\n");
    }

    function close(){
      if ($this->fp) {
        fclose($this->fp);
        $this->fp = NULL;
      }
    }

    function __destruct() {
      $this->close();
    }
  }

Upvotes: 2

Views: 77

Answers (1)

codaddict
codaddict

Reputation: 455152

You need to use double quotes and not single quotes:

$text = "some text \n\n Some more text.";
        ^                              ^

Upvotes: 7

Related Questions