Page 1 of 1
help with \n
Posted: Tue Jul 26, 2005 1:33 pm
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
Posted: Tue Jul 26, 2005 1:56 pm
by hawleyjr
Once again. Please read Posting Code In The Forums!
viewtopic.php?t=21171
Posted: Tue Jul 26, 2005 2:03 pm
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.
Posted: Tue Jul 26, 2005 2:05 pm
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 ]
Posted: Tue Jul 26, 2005 2:07 pm
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...
Posted: Tue Jul 26, 2005 2:09 pm
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";
Posted: Tue Jul 26, 2005 2:17 pm
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?
Posted: Tue Jul 26, 2005 2:22 pm
by andre_c
post the exact error (as jshpro2 has already asked)
Posted: Tue Jul 26, 2005 2:23 pm
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....
Posted: Tue Jul 26, 2005 2:26 pm
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.
Re: help with \n
Posted: Tue Jul 26, 2005 2:31 pm
by nielsene
Code: Select all
if(!$connect||!$selectdb)
{
$error= "e;Error: can not connect to database\\n"e;;
$error.="e;user:\t"e;.$dbuser."e;<br>"e;;
print '<script language="e;javascript"e;>';
print 'alert("e;'.$error.'"e;)';
print '</script>';
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".
Posted: Tue Jul 26, 2005 2:37 pm
by hawleyjr
wow nielsene now that someone put it into php tags I too see the error

Posted: Tue Jul 26, 2005 2:51 pm
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