Harry Baya wrote:
I gather there is an archive for this listServ. How do I get to it? I will explore on my own.

The question I joined this listServ to ask is below. Please let me know if it should be posted in a different list:

I have installed PHP and Apache to run locally on my Windows XP machine. It works! However, when I view a .php web page (i.e. mixing PHP and HTML) the book I have says that "\n" used inside double quotes in a print or echo command should cause a line feed.

It does not do so for me. If I use <br /> I get a line feed, but not with \n. See example below.

<?php
print "I'm just ducky.\n\nhow are you?";
?>

This does NOT put in the line feeds I expect from \n. Why is that? Can I fix it? Is that a browser setting, or maybe a PHP setting?

I noticed that the \t and \r also seem to do nothing. However \$, \\ and \" seem to work as desired. Oddly I found that \$ and $ both put a $ in the web page, both \ and \\ put a single slash in the web page. Help !

Harry


Well technically this list is for install problems but your qestion is easily enough answered :p

So what happens (very crude explaination!) is the text is echoed to the browser, it stores the document with line feeds and all and runs it through the rendering engine to give you your web page output. "\n" does cause a line feed but in the SOURCE of the generated web page not in the visible ouput, as \n, \t, \r and other special characters are not HTML and are ignored by the browser's rendering engine (just as more than one space character is) which is why <br> or <br /> exist to give you your visible line feed :)

So you are simply creating the source code for the webpage (which if you look at it has all the special characters doing their thing), the browser then processes this into the webpage you see minus any non-HTML formatting.

If you place the output within <pre></pre> tags it will probably give you your line feeds though as this causes the browser to render the contained text as it is formatted in the source.

Also if you run the same code with the CLI you will see the new lines and tabs on the terminal output as it is not outputing processed html, just the plain text.

Now onto \, this is the PHP escape character which basically means it nullifies any special meaning that the following characters have in PHP and they are just treated as plain text. So \$variable will be treated as the literal text '$variable' and not the value contained in $variable. This only matters within double quotes where PHP normally replaces any variable with its value. e.g.

<?php
$greeting = 'hello';
$name = 'bob';

echo "$greeting $name!";      // outputs: hello bob!
echo "\$greeting \$name!";    // outputs: $greeting $name!
echo '$greeting $name!';        // outputs: $greeting $name!
?>

and \\n will output the literal text \n (not a line break)

As $ by itself is not a valid variable name and both '\$' and '$' print as '$' (though I suppose \$ should print as \$ given my next sentence). And \ followed by no special characters is treated as a literal \ and \\ is the same; as the first \ escapes the second \ which leaves you with \. To get \\ printed you need \\\ confused? :)

Cheers,

Brad Kowalczyk
www.ibiscode.com

Reply via email to