0

I have started learning PHP. In my first code the newline is not coming properly. I have gone through the PHP doc, but still I'm not getting the problem.

<?php
    # Echoing 
    echo "Hello World to PHP \n";
    echo "Concatenation in PHP is done using ." . "Vivek kumar" . "Learning php";

    # Variable Basics
    $name = "Vivek Kumar";
    $age = 26;
    echo "My name is $name and age is $age .";
    echo 'My name is '. $name . ' and age is ' . $age . '.';
?>

output:

Hello World to PHP Concatenation in PHP is done using .Vivek kumarLearning phpMy name is Vivek Kumar and age is 26 .My name is Vivek Kumar and age is 26.

Vivek Kumar
  • 4,822
  • 8
  • 51
  • 85

3 Answers3

2

I assume you echo the output into a html page you look at with a web browser? In that case the linebreaks do get copied to the output, however they are not visualized as such. This simply is because in html markup a linebreak is something different to a plain text file.


Check about the use of <br> or <br /> linebreaks for such markup. Also php offers the handy nl2br() function for such purpose.

An example for such output (see it here):

Hello World to PHP Concatenation in PHP is done using .
<br />
Vivek kumarLearning phpMy name is Vivek Kumar and age is 26 .
<br />
My name is Vivek Kumar and age is 26.

But in general you should think about whether you really simply want to concatenate such strings when putting them in to html markup. Typically you want to wrap them in (invisible) containers like spans, divs or paragraphs, so that you can control the final layout by using styles (style sheets / css).

An arbitrary example (see it here)

HTML:

<div id="intro">Hello World to PHP Concatenation in PHP is done using .</div>
<h2>Vivek kumarLearning php</h2>
<div class="plain">My name is Vivek Kumar and age is 26 .</div>
<div class="plain">My name is Vivek Kumar and age is 26.</div>

CSS:

body {
    font-size: 130%;
}
#intro {
    font-weight: bold;
    margin-bottom: 10px;
}
.plain {
    font-size: 100%;
}
arkascha
  • 41,620
  • 7
  • 58
  • 90
1

You can use nl2br to convert new line (\n) to line break (<br>).

From http://php.net/manual/en/function.nl2br.php:

string nl2br ( string $string [, bool $is_xhtml = true ] )

Returns string with <br /> or <br> inserted before all newlines (\r\n, \n\r, \n and \r).

Or you can just use line break, as the others have suggested. nl2br is handy when showing text from a form text area.

Thomas Jensen
  • 2,138
  • 2
  • 25
  • 48
0

if you are using this in a browser you should use <br> tag or you should use <pre> I am posting the answer with <pre> and with <br>

**with `<pre>`**

    <?php
        # Echoing 
        echo '<pre>';
        echo "Hello World to PHP "."\n";
        echo "Concatenation in PHP is done using ." . "Vivek kumar" . "Learning php";
        echo '<pre>';

with

             echo "Hello World to PHP "."<br/>";
            echo "Concatenation in PHP is done using ." . "Vivek kumar" . "Learning php";
arif_suhail_123
  • 2,509
  • 2
  • 12
  • 16