Re: Breaking lines

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, October 3, 2006 7:05 am, Deckard wrote:
> I have this code to write three lines in a file (config.php):
>
> $stringData = '$hostname = ' . $hostname . '\n' . '$mysql_username = '
> .
> $mysql_username . '\n' . '$mysql_user_password = ' .
> $mysql_user_password . '\n';
>
> but instead of breaking a line, it appears in the file the string \n
>
> How can i make the line break ?

There are only *TWO* characters that are "special" after a '
\
'

' is used to note the end of the string.
\ is used to indicate that the following ' is *NOT* the end of the
string, but is an embedded '
\ can then also be used to indicate that the following \ is *NOT* a \
to indicate that the following ' is *NOT* the end of the string, but
just a plain old embedded \

Examples:
PHP    Internal string
'\''   a single apostrophe
'\\'   a single backslash
'\\n'  a single backslash followed by 'n'
'\n'   a very confusing way to get a single backslash followed by 'n'

I will not document the "rules" for the characters following a "
because they are MUCH more complex.
http://us3.php.net/manual/en/language.types.string.php

The point, however, is that \n is a newline ONLY inside of ", and not
' because ' is a much more limited vocabulary.

" has a much richer vocabulary, so you could just write:

$stringData = "\$hostname = $hostname\n\$mysql_username =
$mysql_username\n\$mysql_user_password = $mysql_user_password\n";

A cleaner option might be:
$stringData = "hostname = $hostname\n";
$stringData .= "mysql_username = $mysql_username\n";
$stringData .= "mysql_password = $mysql_user_password\n";

Presumably this was for a human to read anyway, so the leading $ on
the label was not crucial...

YMMV

-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux