user2481982
user2481982

Reputation: 101

PHP: Character escaping not working with double quotes unless there is a <pre> tag?

Please can someone tell me where I'm going wrong?

//outputs : Hi, my name is Jackal I like PHP
<?php
echo "Hi, my name is Jackal \nI like PHP";
?>

Whereas if I use pre tag

//outputs: Hi, my name is Jackal
//         I like PHP
<pre>
<?php
echo "Hi, my name is Jackal \nI like PHP";
?>

Can someone please explain why character escaping isn't working? thanks

Upvotes: 0

Views: 97

Answers (6)

Kevin
Kevin

Reputation: 6701

The newline character in php actually echos a new line itself.

However, the output of php's echo statement is still the source code of the web browser. Hence, the browser condenses all the extra line breaks and spaces. For example:

<p>Hello
    there</p>

outputs

Hello there

despite the white space. To get around this issue, use the <br>/<br/> tag to tell the browser to make a newline, instead of tell php to make a newline.

Upvotes: 0

Dru
Dru

Reputation: 556

<br> 

is HTML.

<br />

is XHTML (recommended).

\n

is a newline in the code (works while writing to file, but doesn't output to the screen).

alternately you can use nl2br as below

echo nl2br("Hi, my name is Jackal \nI like PHP");

Upvotes: 0

BLaZuRE
BLaZuRE

Reputation: 2406

PHP is interpreting that line break. Look in the source code of the webpage. You'll see that you have 2 lines in the source.

However, HTML handles line breaks differently. To perform a line break in HTML, use the <br> tag. This will make the HTML output over 2 lines on the webpage itself. However, in the source code, it will still appear as a single line (unless you add that line break).

What <pre> is doing is telling the HTML engine to output the exact string as-is since the text is preformatted, including interpreting that line break. Also note, you should add a closing </pre> tag at the end of the block that is preformatted.

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324750

If you have this in your HTML:

<p>Hello,
World!</p>

Does it appear on one line, or two?

Usually, the answer is one: whitespace is condensed into a single space.

<pre>, however, has the default CSS of white-space:pre, which does NOT condense whitespace.

You should echo "<br />", or apply white-space:pre (or, even better, white-space:pre-wrap) to the container element.

Upvotes: 1

patman
patman

Reputation: 2840

Character escaping is working. Look at the source code, there you'll find the new line. In order to make it visible in a browser you need an html tag (<br />) or the wrapping <pre>

Upvotes: 0

anubhava
anubhava

Reputation: 785541

Because in your browser \n shows just a whitespace and does not appear as line break unless you use pre-formatted HTML tag <pre>.

e.g. following will display with a line break in browser:

echo "Hi, my name is Jackal <br />I like PHP";

or this one also:

echo "<pre>Hi, my name is Jackal \nI like PHP</pre>";

As you can make out that <pre> tag is for displaying pre-formatted text hence \n does show as line break.

Upvotes: 0

Related Questions