Androelpha
Androelpha

Reputation: 387

How To echo a line break in the browser?

I dont know how to echo a new line in PHP. I have tried echo "\n" but it does not work.

I want to echo new lines in the following code.

Code:

if (file_exists($fName)) {
   echo "CreationTime: ".$CreationTime.
        "CurrentTime: ".$CurrentTime.
        "after ".($fLifeTime)." Days from Creation: ".$fAge;
   }

Upvotes: 2

Views: 41104

Answers (4)

Boby
Boby

Reputation: 822

Maybe try to use PHP_EOL and nl2br()?

if (!file_exists($fName)) {
    $info = "CreationTime: " . $CreationTime . PHP_EOL .
        "CurrentTime: ". $CurrentTime . PHP_EOL .
        "after ". ($fLifeTime)." Days from Creation: " . $fAge;

    echo nl2br($info);
}

Upvotes: -1

IF you are looking this output from a browser, you must use <br /> tag or, put <pre> before your echo.

For new line
example.

if (file_exists($fName)) {
   echo "CreationTime: ".$CreationTime. "<br />".
        "CurrentTime: ".$CurrentTime. "<br />" .
        "after ".($fLifeTime)." Days from Creation: ".$fAge;
   }

pre example

 if (file_exists($fName)) {
      echo "<pre>";
       echo "CreationTime: ".$CreationTime. PHP_EOL .
            "CurrentTime: ".$CurrentTime. PHP_EOL .
            "after ".($fLifeTime)." Days from Creation: ".$fAge;
       }

Upvotes: 8

FrontEnd Expert
FrontEnd Expert

Reputation: 5803

Try to do like this:--

if (file_exists($fName)) {
   echo "CreationTime: ".$CreationTime.
        "CurrentTime: ".$CurrentTime.
        "after ".($fLifeTime)." Days from Creation: ".$fAge;
echo "<br/>"; //break to new line.
   }

Upvotes: -1

Rukmi Patel
Rukmi Patel

Reputation: 2561

you can do it like this

if (file_exists($fName)) {
    echo "CreationTime: ".$CreationTime.
    "<br />CurrentTime: ".$CurrentTime.
    "<br />after ".($fLifeTime)." Days from Creation: ".$fAge;
}

Upvotes: 0

Related Questions