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
|