Sep 22, 2010

Posted by in PHP, Technology | 3 comments

\n is not creating a new line in PHP

It was really crazy and I was clue-less why \n is not creating a new line when I am using it.

Is the same happening for you, are you trying to use it in the following ways ? Please check the following code..

$my_data = 'Hello User, \n Thank your for visiting my blog \n You will know what is the issue pretty soon';

$fh = fopen('myfile.txt', 'w') or die("can't open file");

fwrite($fh, $my_data);

fclose($fh);

/*
OR you are trying to echo that data inside a text area like below
*/


echo '<textarea rows="10" cols="40" name="mytext">'.$my_data.'</textarea>';

The above code doesn’t work for you, and you see \n as it is in the text area even in the new file after you wrote the data, is that what with you too ?

No worry here, if you see we have enclosed the data inside single quote, well that is where it goes, when we write \n inside single quote and try to echo it or write it to any file it will write \n as it is with out creating any new line, make sure you are enclosing the data in double quote and then it will work, for example the following code will work.

//See we are using double quote here.

$my_data = "Hello User, \n Thank your for visiting my blog \n You will know what is the issue pretty soon";

$fh = fopen('myfile.txt', 'w') or die("can't open file");

fwrite($fh, $my_data);

fclose($fh);

/*
OR you are trying to echo that data inside a text area like below
*/


echo '<textarea rows="10" cols="40" name="mytext">'.$my_data.'</textarea>';
  1. i) as already discussed in this article using “\n” character in double quote will prints new line.
    echo “Hello world.\nMy Program”;

    ———————————————–

    ii) you can use “” tag inside double quote or single quote to print newline.
    echo “Hello world.My Program”;
    or
    echo ‘Hello world.My Program’;

    ———————————————–

    iii) this is very useful while printing source code as it is in newline.
    $var = <<<'EOT'

    EOT;
    echo nl2br(htmlentities($var));

    ———————————————–

    iv) this is very useful while printing variables.
    $number = 9841866445;
    $var = <<<"end"
    Your Number ::
    end;
    echo $var;

    Hope this will be helpful.
    Madan Sapkota (Dharam sir Follower)
    Thank You.

  2. i) as already discussed in this article using "\n" character in double quote will prints new line.
    echo "Hello world.\nMy Program";

    ———————————————–

    ii) you can use "<br />" tag inside double quote or single quote to print newline.
    echo "Hello world.<br />My Program";
    or
    echo ‘Hello world.<br />My Program’;

    ———————————————–

    iii) this is very useful while printing source code as it is in newline.
    $var = <<<‘EOT’

    <?php
    echo "Hello PHP";
    ?>
    EOT;
    echo nl2br(htmlentities($var));

    ———————————————–

    iv) this is very useful while printing variables.
    $number = 9841866445;
    $var = <<<"end"
    Your Number :: <input type="text" value="$number" />
    end;
    echo $var;

    Hope this will be helpful.
    Madan Sapkota (Dharam sir Follower)
    Thank You.

  3. Nice post. I actually took some time to go through the php.net manual. I have not gone through it all yet, but I found about this issue in the “string” chapter.

Leave a Reply