help with \n

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
php12342005
Forum Commoner
Posts: 79
Joined: Mon Mar 21, 2005 3:35 am

help with \n

Post by php12342005 »

followings are php code, error on line 3 because of the \n.
I can not understand that why \t is ok but \n is error?

headache for php.
(\n is for breaking a new line)

if(!$connect||!$selectdb)
{
$error= "Error: can not connect to database\n";//line 3
$error.="user:\t".$dbuser."<br>";

print '<script language="javascript">';
print 'alert("'.$error.'")';
print '</script>';
return false;
}

why and how?
thanks
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Once again. Please read Posting Code In The Forums!

viewtopic.php?t=21171
php12342005
Forum Commoner
Posts: 79
Joined: Mon Mar 21, 2005 3:35 am

Post by php12342005 »

forum is for providing direct assistances, I don't like to spend a lots of time to read long articles and get zero answer.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Post the error you're getting word for word

EDIT: I'll sum the 'long article' up for you, surround your code with [ php ] and [ /php ]
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

php12342005 wrote:forum is for providing direct assistances, I don't like to spend a lots of time to read long articles and get zero answer.
People are here to help you. Help us Help you...
Fimble
Forum Newbie
Posts: 12
Joined: Tue Jul 26, 2005 10:13 am

Post by Fimble »

please excuse me as i only started this PHP stuff the other day but i think you will find your having and error on both lines BUT the results your getting are confusing....

Im guessing your commenting out the line you marked "line 3" ... in doing so the line is ignored and the one bellow it becomes line 3 (but your expecting it to still be line 4)?

To test this i made a simple php program:

Code: Select all

<?php
echo " Test \n";
echo " Test2 \t";
echo " Test3 \t";
?>
ran it with an error on the 1st line... then commended out the 1st line and still got an error on the first line ...
Showing me the commenting means its ignored this is probably why the error on line 3 and 4 appear as both errors on line 3 as commenting out the current line 3 makes the current line 4 become line 3.

to fix it place the \n's and \t's outside html tags like so

Code: Select all

echo " <p>Test</p> \n";
echo " <p>Test2</p> \t";
echo " <p>Test3</p> \t";
php12342005
Forum Commoner
Posts: 79
Joined: Mon Mar 21, 2005 3:35 am

Post by php12342005 »

the code is not related to my question.
in my post, why does \n in line 3 cause an error?
did you test my code?
User avatar
andre_c
Forum Contributor
Posts: 412
Joined: Sun Feb 29, 2004 6:49 pm
Location: Salt Lake City, Utah

Post by andre_c »

post the exact error (as jshpro2 has already asked)
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

php12342005 wrote:the code is not related to my question.
in my post, why does \n in line 3 cause an error?
did you test my code?
I just tested your code, no error in the php code.... Exactly what error message your you getting....
Fimble
Forum Newbie
Posts: 12
Joined: Tue Jul 26, 2005 10:13 am

Post by Fimble »

No error in php here ...
I tried it deleting the "if" satement to make sure it was getting into the main part of the code.

thats why i tried it with the test code i knocked up to try and duplicate the error....
but your code runs fine.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Re: help with \n

Post by nielsene »

Code: Select all

if(!$connect||!$selectdb)
{	
$error=	&quote;Error: can not connect to database\\n&quote;;
$error.=&quote;user:\t&quote;.$dbuser.&quote;&lt;br&gt;&quote;;

print '&lt;script language=&quote;javascript&quote;&gt;';
print 'alert(&quote;'.$error.'&quote;)';
print '&lt;/script&gt;';
return false;
}
OK, notice the double \\ on line 3 inside the quotes.

You want the the "\n" to end up inside the javascript, so that the javascript inserts a line break. You don't want PHP to replace the \n with a linebreak. Therefore you have to escape the slash, so that javascript see "\n".
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

wow nielsene now that someone put it into php tags I too see the error :roll:
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

php12342005 now I'm telling you! hawleyjr is right. Read how to post code and at the very least use

Code: Select all

tags. You have nearly 60 posts, I'd expect you to have got the message by now
Post Reply