View Full Version : how to use \n ?

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.

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

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

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;


But just echoing it out would produce;


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

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

take this example..


$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...


$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...

<td>test data</td>
<td>test data</td>
<td>test data</td>

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.

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

now i know