PDA

View Full Version : how to use \n ?


mampwamp
08-12-2010, 07:11 AM
hi, what is the meaning of \n ?

i check from php.net it means linefeed (LF or 0x0A (10) in ASCII)

but i am not exactly get the meaning of it.

can give any example of it.


<?php
function foo() {
echo "In foo()<br />\n";
}

function bar($arg = '')
{
echo "In bar(); argument was '$arg'.<br />\n";
}

Corrosive
08-12-2010, 07:29 AM
As far as I understand it /n is used to help make output HTML look the same as if you had hand coded it, i.e. with line breaks. Say you dynamically output your menu you'd want the code (when view>> source is used) to look like;

<ul>
<li>One</li>
<li>Two</li>
<li>Three</li>
</ul>

But just echoing it out would produce;

<ul><li>One</li><li>Two</li><li>Three</li></ul>

Putting /n at the end of the line just gives your code a new line to make it look nice and neat :)

davidj
08-12-2010, 10:24 AM
Toby is correct

take this example..

<?php

$table = "<table>";

$table .= "<tr>";

$table .= "<td>test data</td><td>test data</td><td>test data</td>";

$table .= "</tr>";

$table .= "</table>";

echo $table;

?>

if you view source after you have processed it the source code looks like...

<table><tr><td>test data</td><td>test data</td><td>test data</td></tr></table>

If you add \n like this...


<?php

$table = "<table>\n";

$table .= "<tr>\n";

$table .= "<td>test data</td>\n<td>test data</td>\n<td>test data</td>\n";

$table .= "</tr>\n";

$table .= "</table>\n";



echo $table;

?>

The source code looks like this...

<table>
<tr>
<td>test data</td>
<td>test data</td>
<td>test data</td>
</tr>
</table>


Its just a better way to lay out your source code. Easier to read

Note that \n will only render in double quotes. If you use single quotes they will be rendered as a literal part of the string.

mampwamp
08-12-2010, 02:48 PM
thank..good example.

now i know