Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 30th, 2003, 04:12 PM
Registered User
 
Join Date: Jun 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Noobie Question on Carriage Return

I have been attempting to learn PHP through the Beggining Book, (I have no coding experience). I just wanted to know why the examples in the book use both the "<BR>" and the "\n" in the same echo statement? an example would be like this, (from page 298 I think):

\\echo("Our two number are $a and $b<BR>\n");

Thanks
 
Old June 30th, 2003, 04:32 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by rsteph7
Code:
echo("Our two number are $a and $b<BR>\n");
The <BR> is an HTML tag, albeit slightly incorrect -- new HTML standards require all tags to be lower-case. To conform to XHTML rules, it should also be closed.

The correct version is <br />.

The \n is an escape character which inserts a newline character in a string. Since HTML treats all consecutive whitespace characters as a single space entity within a document, you need to use the <br/> tag to get a single line break in your text.


Take care,

Nik
http://www.bigaction.org/
 
Old June 30th, 2003, 11:05 PM
Authorized User
 
Join Date: Jun 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to natmaster Send a message via AIM to natmaster Send a message via MSN to natmaster
Default

just one question - what is XHTML? i've always wondered that

----------------------------
http://aeonofdarkness.com
 
Old July 1st, 2003, 12:11 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by natmaster
just one question - what is XHTML? i've always wondered that
XHTML is an HTML document that conforms to XML rules -- all tags must be properly balanced and nested.

For example, you can't have this;
Code:
<html>
<body>
<p>
Hello, world<br>
How are you?
</body>
</html>
The correct way to do that is:
Code:
<html>
<body>
<p>
Hello, world<br />
Hw are you?
</p>
</body>
</html>

Take care,

Nik
http://www.bigaction.org/
 
Old July 1st, 2003, 04:25 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

May I add to Mick's reply by saying that the reason you'd use "\n"'s in your output is to format the markup you output to the browser. This is useful where the markup elements are heavily nested, for instance in tabular data. Although the markup:

<table><tr><td></td><th id="c2">Meals</th><th id="c3">Hotels</th><th id="c4">Transport</th></tr><tr><td id="r2">San Jose</th><td></td><td></td><td></td></tr> ... etc.

Will technically display correctly in the browser, the output:

<table>
  <tr>
    <td></td>
    <th id="c2">Meals</th>
    <th id="c3">Hotels</th>
    <th id="c4">Transport</th>
  </tr>
  <tr>
    <td id="r2">San Jose</th>
    <td></td>
    <td></td>
    <td></td>
  </tr>

... etc.

...is much easier to read when viewing source. However, if you were generating the latter half of, say, that first table row using a script like this:

$rowinc = 1;
while( $row = mysql_fetch_assoc($result)){
   echo "<th id=\"c" . $rowinc . "\">" . $row['column_head'] . "</td>";
   %rowinc++;
}


It will output the <th> elements in one line, as before, i.e.:

<th id="c2">Meals</th><th id="c3">Hotels</th><th id="c4">Transport</th>

So it might be better to rewrite your code thus:

$rowinc = 1;
while( $row = mysql_fetch_assoc($result)){
   // Insert for white spaces at start of line and a Cr&Lf combination at end.
   echo " <th id=\"c" . $rowinc . "\">" . $row['column_head'] . "</td>\r\n";
   %rowinc++;
}


This ensures that the indentation of your table is preserved, making it easier to read - and sometimes even making it easier to debug!

Indeed, this sort of formatting becomes crucially important later on, if you start using MIME mail, where you start writing the headers using script. Here, the use of the "\r\n" escape sequence (for the "carriage and line feed" combination) is not optional and can have a fundamental effect on whether your headers are interpreted correctly or not.

In summary
With server side scripting there are two sets of "code" involved: the server-side scripts, and the client-side mark-up, and it takes relatively little effort to ensure that both are indented clearly to make the purpose of each clearly legible, easy to debug, and look less like it was churned out automatically, in a "b*gger it, it works, don't it?" kind of a fashion :).

Dan

Quote:
quote:Originally posted by rsteph7
 I have been attempting to learn PHP through the Beggining Book, (I have no coding experience). I just wanted to know why the examples in the book use both the "<BR>" and the "\n" in the same echo statement? an example would be like this, (from page 298 I think):

\\echo("Our two number are $a and $b<BR>\n");

Thanks





Similar Threads
Thread Thread Starter Forum Replies Last Post
Checking for a Carriage return darkhalf Classic ASP Databases 2 November 5th, 2007 06:15 AM
insert carriage return darkhalf Javascript 0 April 10th, 2007 02:42 PM
about carriage return... edu1980 XSLT 1 July 5th, 2006 04:11 AM
New Line/Carriage Return interrupt Javascript How-To 7 October 3rd, 2004 06:27 AM
HELP Inserting Carriage Return orangem Classic ASP Databases 8 July 5th, 2004 08:36 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.