Question about new lines [message #175489] |
Sun, 02 October 2011 10:00 |
|
I come from a scripting background so am not unfamiliar
with the concept of CR/LF characters. I am in the process
of learning PHP via the following tutorial
http://mobilitystudies.com/mob/images/stories/PHP_Tutorial_From_beginner_to _master.pdf
Currently I have "hello.php" with the following lines of code
-------------------------
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
elseif ($d=="Sun")
echo "Have a nice Sunday!";
else echo "Have a nice day!";
echo "\nhello boss"."\t"."does this work".PHP_EOL;
echo "again";
?>
</body>
</html>
---------------------------------
When I load this in my browser I see the following
------------------------
Have a nice Sunday! hello boss does this work again
-------------------------
And if I look at the page source (using Firefox)
--------------------------
<html>
<body>
Have a nice Sunday!
hello boss does this work
again</body>
</html>
------------------------
Should the "\n" not push a NewLine out to the output?
I did some research and found that the PHP_EOL seems to be what is
recommended since it is platform independant. But it doesn't work
either.
Can anyone suggest or point out what I am doing wrong, or is this a flaw
in php?
Thanks
--
*************************************************************************** *****
* Usenet Impovement Project http://nacs.dyndns-office.com/usenet
* Bakers trade bread recipes on a knead-to-know basis.
*************************************************************************** ******
|
|
|
|
Re: Question about new lines [message #175491 is a reply to message #175489] |
Sun, 02 October 2011 11:21 |
|
Linus Flustillbe wrote:
> I come from a scripting background so am not unfamiliar
> with the concept of CR/LF characters. I am in the process
> of learning PHP via the following tutorial
>
> http://mobilitystudies.com/mob/images/stories/PHP_Tutorial_From_beginner_to _master.pdf
>
> Currently I have "hello.php" with the following lines of code
>
> -------------------------
> <html>
> <body>
> <?php
> $d=date("D");
> if ($d=="Fri")
> echo "Have a nice weekend!";
> elseif ($d=="Sun")
> echo "Have a nice Sunday!";
> else echo "Have a nice day!";
> echo "\nhello boss"."\t"."does this work".PHP_EOL;
> echo "again";
> ?>
> </body>
> </html>
> ---------------------------------
> When I load this in my browser I see the following
> ------------------------
> Have a nice Sunday! hello boss does this work again
> -------------------------
> And if I look at the page source (using Firefox)
> --------------------------
> <html>
> <body>
> Have a nice Sunday!
> hello boss does this work
> again</body>
> </html>
> ------------------------
> Should the "\n" not push a NewLine out to the output?
> I did some research and found that the PHP_EOL seems to be what is
> recommended since it is platform independant. But it doesn't work
> either.
>
> Can anyone suggest or point out what I am doing wrong, or is this a flaw
> in php?
>
No, its a flaw in your understanding of HTML which does not recognise
white space including new lines.
use <BR> instead.
> Thanks
>
|
|
|
|
Re: Question about new lines [message #175494 is a reply to message #175490] |
Sun, 02 October 2011 12:36 |
|
On 2011-10-02, Jeff Gaines <jgaines_newsid(at)yahoo(dot)co(dot)uk> wrote:
> On 02/10/2011 in message <4e886e7b$0$5356$9a566e8b(at)news(dot)aliant(dot)net> Linus
> Flustillbe wrote:
>
>> Should the "\n" not push a NewLine out to the output?
>> I did some research and found that the PHP_EOL seems to be what is
>> recommended since it is platform independant. But it doesn't work
>> either.
>
> I would use:
>
> echo "<br />";
>
> Which will force a line break in HTML.
>
or we could use a <p> to force a paragraph switch.
Thanks all for your help, I have it working now.
--
*************************************************************************** *****
* Usenet Impovement Project http://nacs.dyndns-office.com/usenet
* Bakers trade bread recipes on a knead-to-know basis.
*************************************************************************** ******
|
|
|
Re: Question about new lines [message #175496 is a reply to message #175489] |
Sun, 02 October 2011 15:08 |
|
On Sun, 02 Oct 2011 14:00:27 +0000, Linus Flustillbe wrote:
> I come from a scripting background so am not unfamiliar with the concept
> of CR/LF characters. I am in the process of learning PHP via the
> following tutorial
A web browser considers new lines within the text content of html
documents to be white space, just as it considers spaces and tabs within
the text content of html documents to be white space.
Line breaks are signalled by "<br>" or "<br />" (which you use depends on
the dtd you declare, if you don't declare a dtd use <br>) and paragraphs
are wrapped in "<p></p>". In html, these elements control forced line
breaks.
Carriage returns in the text content of html documents don't generate
line breaks.
If you're using a scripting language such as php to generate
presentational markup such as html, you need to make sure you understand
what correct presentational markup looks like first.
Rgds
Denis McMahon
|
|
|
Re: Question about new lines [message #175497 is a reply to message #175489] |
Sun, 02 October 2011 17:52 |
|
Linus Flustillbe schrieb:
> -------------------------
> <html>
> <body>
> <?php
> $d=date("D");
> if ($d=="Fri")
> echo "Have a nice weekend!";
> elseif ($d=="Sun")
> echo "Have a nice Sunday!";
> else echo "Have a nice day!";
> echo "\nhello boss"."\t"."does this work".PHP_EOL;
> echo "again";
> ?>
> </body>
> </html>
> ---------------------------------
> When I load this in my browser I see the following
> ------------------------
> Have a nice Sunday! hello boss does this work again
As was to be expected on a Sunday.
> -------------------------
> And if I look at the page source (using Firefox)
> --------------------------
> <html>
> <body>
> Have a nice Sunday!
> hello boss does this work
> again</body>
> </html>
> ------------------------
As was to be expected on a Sunday.
> Should the "\n" not push a NewLine out to the output?
Well it does, doesn't it? Without the "\n" and the PHP_EOL your source
would look like this:
> Have a nice Sunday!hello boss does this workagain
The output of PHP is the HTML source code. How the browser renders that
is another matter. If you want the line breaks to appear in the
browser's output, do as the other posts suggest.
Greetings,
Thomas
--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
|
|
|
|
Re: Question about new lines [message #175500 is a reply to message #175496] |
Sun, 02 October 2011 20:57 |
Peter H. Coffin
Messages: 245 Registered: September 2010
Karma: 0
|
Senior Member |
add to buddy list ignore all messages by this user
|
|
On 02 Oct 2011 19:08:26 GMT, Denis McMahon wrote:
> Line breaks are signalled by "<br>" or "<br />" (which you use depends on
> the dtd you declare, if you don't declare a dtd use <br>) and paragraphs
> are wrapped in "<p></p>". In html, these elements control forced line
> breaks.
Well, to be REALLY pedantic, the <p> marker doesn't force a line break.
It does whatever the paragraph break is supposed to do, whether that's
vertical boundry space, boundry with indent, indent alone, outdent,
continuous with pilcrow, etc. It's all dependant on what the browser is
set up to support, what styles are in effect, whether they're from the
site or overridden by the user, etc. HTML is not layout.
--
59. I will never build a sentient computer smarter than I am.
--Peter Anspach's list of things to do as an Evil Overlord
|
|
|
|
Re: Question about new lines [message #175503 is a reply to message #175489] |
Mon, 03 October 2011 01:15 |
|
> I come from a scripting background so am not unfamiliar
> with the concept of CR/LF characters. I am in the process
> of learning PHP via the following tutorial
A newline in output interpreted as HTML is rendered no differently
from a space or series of spaces. It shows up as a newline in "view
source" of browsers but not in rendered HTML.
If you want to cause a line break, do it in HTML. <br> and <p> are
commonly used for this. Also, there's <tr> inside tables, which
does a bit more than just a line break.
If you output your text with header("Content-Type: text/plain")
(read the documentation about how to use this: HTTP headers have
to come *before* any text sent to the browser. Even a single space
or newline.) or otherwise get the web server to call it a plain
text file rather than HTML, newlines will be visible.
|
|
|
|
Re: Question about new lines [message #175506 is a reply to message #175505] |
Mon, 03 October 2011 05:40 |
|
Jeff Gaines wrote:
> The Natural Philosopher wrote:
>> Jeff Gaines wrote:
>>> On 02/10/2011 in message <4e889326$0$5371$9a566e8b(at)news(dot)aliant(dot)net>
>>> Linus Flustillbe wrote:
>>>> or we could use a <p> to force a paragraph switch.
>>>> Thanks all for your help, I have it working now.
>>> Don't forget you should close a <p> tag with </p>
>> I have never seen that ever in any source
>
> Really?
>
> http://www.w3schools.com/tags/tag_p.asp
W3Schools can be safely and must be strongly recommended against for use as
a reliable resource on Web development. When in doubt, consult the official
Specifications:
<http://www.w3.org/TR/html401/struct/text.html#edef-P>
However, the P/p element does have an _end_ tag, and it is a good idea to
use it so that the paragraph will be delimited *explicitly*. In XHTML, it
is mandatory anyway, as XHTML is an application of XML and well-formedness
requires either an end tag or SHORTTAG syntax (as below).
> That would be an advantage of using <br /> for the OP.
<br /> is XHTML SHORTTAG (not considering HTML SHORTTAG, which is widely
disregarded by browsers); <br>, <bR>, <Br> or <BR> is HTML. PHP has the
nl2br() function to make printing newlines as BR/br elements in (X)HTML
easier.
BR/br usually suffices for debugging purposes. However, semantics are
important: If the intention is to make a paragraph, use the P/p element; if
it is to make an unconditional *line break* *within* *a* *text* *paragraph*,
use the BR/br element.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7(at)news(dot)demon(dot)co(dot)uk>
|
|
|
|
|
|
Re: Question about new lines [message #175512 is a reply to message #175508] |
Mon, 03 October 2011 08:51 |
|
Jeff Gaines wrote:
> Jerry Stuckle wrote:
>> Which isn't valid on html - only xhtml or xml.
>
> There seems to be confusion about whether or not the / is optional.
Yes, that only *seems* to be so.
> In reality, of course, all browsers honour it.
Your logic is flawed. You simply cannot know about *all* browsers.
AISB, *some* browsers, perhaps the majority in terms of market share, do not
implement the SGML Declaration for HTML 4.01 [1] properly, which specifies
the SHORTTAG syntax for HTML so that `<br />' is equivalent to `<br>>' in
HTML (that allows, for example `<h1/My Title/' instead of `<h1>My
Title</h1>') [2]. But the W3C Validator [3] does, which is why, e. g.,
<link … rel="…" … />
is _not_ a Valid HTML 4.01 fragment (as `>' would be character data which
is not allowed within the `HEAD' element).
This has led to the *current* HTML5 Working Draft regarding the trailing `/'
between `<' and the corresponding `>' of a start tag as allowed and optional
in the HTML syntax of HTML5 [4,5]. But you SHOULD NOT rely on that
outside of HTML5, perhaps not even in HTML5 until it has become stable.
HTH
PointedEars
___________
[1] <http://www.w3.org/TR/html401/sgml/sgmldecl.html>
[2] <http://www.w3.org/MarkUp/html3/HTMLandSGML.html>
[3] <http://validator.w3.org/>
[4] <http://www.w3.org/TR/2011/WD-html5-20110525/syntax.html#start-tags>
[5] <http://www.w3.org/TR/2011/WD-html5-20110525/introduction.html#html-vs-
xhtml>
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
|
|
|
Re: Question about new lines [message #175514 is a reply to message #175510] |
Mon, 03 October 2011 09:05 |
Jeff Gaines
Messages: 11 Registered: October 2011
Karma: 0
|
Junior Member |
add to buddy list ignore all messages by this user
|
|
On 03/10/2011 in message <j6cau7$hu5$1(at)dont-email(dot)me> Jerry Stuckle wrote:
> Whether browsers honor it or not is immaterial. It is directly against
> the HTML spec, and attempts to validate pages with such ops will fail.
> Just because browsers currently support it does not mean future browsers
> will support something that violates the spec. It's also very sloppy
> programming.
I use it on all my web sites and they all validate 100%.
--
Jeff Gaines Wiltshire UK
If you ever find something you like buy a lifetime supply because they
will stop making it
|
|
|
Re: Question about new lines [message #175515 is a reply to message #175514] |
Mon, 03 October 2011 09:49 |
|
At 3 Oct 2011 13:05:18 GMT "Jeff Gaines" <jgaines_newsid(at)yahoo(dot)co(dot)uk> wrote:
>
> On 03/10/2011 in message <j6cau7$hu5$1(at)dont-email(dot)me> Jerry Stuckle wrote:
>
>> Whether browsers honor it or not is immaterial. It is directly against
>> the HTML spec, and attempts to validate pages with such ops will fail.
>> Just because browsers currently support it does not mean future browsers
>> will support something that violates the spec. It's also very sloppy
>> programming.
>
> I use it on all my web sites and they all validate 100%.
They would with a DOCTYPE like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Note: 'XHTML' above...
*XHTML* requires the </p> tag and <br /> and would raise at least a
warning if </p> were missing or there were br's with out the / (<br>).
Plain HTML4 does not need the </p> and should not support the <br />
tags (whether browsers browsers honor it or not).
--
Robert Heller -- 978-544-6933 / heller(at)deepsoft(dot)com
Deepwoods Software -- http://www.deepsoft.com/
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments
|
|
|
|
|
|
Re: Question about new lines [message #175519 is a reply to message #175518] |
Mon, 03 October 2011 11:14 |
Jeff Gaines
Messages: 11 Registered: October 2011
Karma: 0
|
Junior Member |
add to buddy list ignore all messages by this user
|
|
On 03/10/2011 in message <j6cigo$4r5$3(at)dont-email(dot)me> Jerry Stuckle wrote:
> On 10/3/2011 10:11 AM, Jeff Gaines wrote:
>> On 03/10/2011 in message
>> <PqednU2_ou72IBTTnZ2dnUVZ_tmdnZ2d(at)posted(dot)localnet> Robert Heller wrote:
>>
>>> They would with a DOCTYPE like this:
>>
>> It's actually:
>> <!DOCTYPE html>
>>
>> As I don't see any point in using anything other than HTML5 on new pages
>> nowadays.
>>
>> The start page should be:
>> http://validator.w3.org/check?uri=http%3A%2F%2Fwww.jgaines.me.uk%2Findex.ph p
>>
>>
>> The "Album" page is a mess at the moment, but will have to wait a while
>> to be sorted.
>>
>
> Info Using experimental feature: HTML5 Conformance Checker.
>
> The validator checked your document with an experimental feature: HTML5
> Conformance Checker. This feature has been made available for your
> convenience, but be aware that it may be unreliable, or not perfectly up
> to date with the latest development of some cutting-edge technologies. If
> you find any issues with this feature, please report them. Thank you.
>
It's the best available at the moment.
> Plus your DOCTYPE does not list a DTD
Not needed, it's HTML5.
--
Jeff Gaines Wiltshire UK
The facts, although interesting, are irrelevant
|
|
|
|
Re: Question about new lines [message #175528 is a reply to message #175489] |
Tue, 04 October 2011 09:38 |
|
On 02 Oct 2011 14:00:27 GMT, Linus Flustillbe
<admin(at)nacs(dot)dyndns-office(dot)com> wrote:
> I come from a scripting background so am not unfamiliar
> with the concept of CR/LF characters. I am in the process
> of learning PHP via the following tutorial
> [SNIP]
> Can anyone suggest or point out what I am doing wrong, or is this a flaw
> in php?
>
> Thanks
Simple Answer:
\n is PHP for breaking a line. It is not translated as the HTML <BR>
as you (and I when I first started using PHP) had been expecting. It
will format your HTML source with new lines - without it all the
source would run together on one line.
To have the browser break a line you need to still each to the browser
an HTML line break <BR>.
So examples:
code:
echo "1, 2, 3, 4, \n5, 6, 7";
output:
1, 2, 3, 4, 5, 6, 7
source view:
1, 2, 3, 4,
5, 6, 7
--------------------------------------------
code:
echo "1, 2, 3, 4, \n<BR>5, 6, 7";
output:
1, 2, 3, 4,
5, 6, 7
source view:
1, 2, 3, 4,
<BR>5, 6, 7
--------------------------------------------
code:
echo "1, 2, 3, 4, \n<BR>\n5, 6, 7";
output:
1, 2, 3, 4,
5, 6, 7
source view:
1, 2, 3, 4,
<BR>
5, 6, 7
Hope that makes it simple and clear.
Mike
|
|
|
|
|
|