davidp Posted July 10, 2007 Share Posted July 10, 2007 This is kind of a simple bug, but I'm not sure why it's happening. Here is a code snippet: function DisplaySideBar ( $pageNumber ) { $SideBarBeginning = "<h3>Sections:</h3><p>"; $SideBarEnding = "</p><br><br>"; print $SideBarBeginning; switch ( $pageNumber ) { case 0: print '<a class="sidelink" href="http://www.lds.org">LDS Church</a><span class="hide"> | </span>\n'; print '<a class="sidelink" href="http://www.byu.edu">BYU</a><span class="hide"> | </span>'; print '<a class="sidelink" href="http://validator.w3.org/check/referer">Valid XHTML</a><span class="hide"> | </span>'; print '<a class="sidelink" href="http://jigsaw.w3.org/css-validator/check/referer">Valid CSS</a>'; break; case 1: print '<p><a class="sidelink" href="index.php?page=1§ion=0">Projects</a><span class="hide"> | </span>'; print '<a class="sidelink" href="index.php?page=1§ion=1">Source Code</a><span class="hide"> | </span>'; print '<a class="sidelink" href="index.php?page=1§ion=2">Contest Problems</a><span class="hide"> | </span>'; print '<a class="sidelink" href="index.php?page=1§ion=3">Tutorials</a><span class="hide"> | </span>'; print '<a class="sidelink" href="index.php?page=1§ion=4">Programming Links</a></p>'; break; case 2: break; case 3: break; case 4: break; case 5: break; } print $SideBarEnding; } You will notice that after the first "print" statement inside of the switch block, I am printing a \n newline. I do this because I want there to be a newline in the source file sent to the client's browser, but of course not a newline in the HTML itself (or else I would have used an HTML "br" tag). Anyways...for some reason the characters "\n" are actualling being printed onto the HTML page and displayed in the browser. I tried using printf, but it didn't work either. How can I fix that? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 10, 2007 Share Posted July 10, 2007 you must use double quotes and not single quotes when you use whitespace characters (\n, \r \r\n). When you sue whitespace characters in single quotes PHP will treat them as normal text and not there special meaning. You'll have to change this line: print '<a class="sidelink" href="http://www.lds.org">LDS Church</a><span class="hide"> | </span>\n'; to: print "<a class=\"sidelink\" href=\"http://www.lds.org\">LDS Church</a><span class=\"hide\"> | </span>\n"; Quote Link to comment Share on other sites More sharing options...
LiamProductions Posted July 10, 2007 Share Posted July 10, 2007 Im not sure, but it works when i use echo not Print. and i use " Quote Link to comment Share on other sites More sharing options...
clanstyles Posted July 10, 2007 Share Posted July 10, 2007 in php in order to actually have a quote in a sentence any time you have to do a \ first. Like this echo "\"Hello\""; is really: "Hello" This also works with \' to make the ' character. Quote Link to comment Share on other sites More sharing options...
TreeNode Posted July 10, 2007 Share Posted July 10, 2007 wildteen88 is right, but also keep in mind that anything in double quotes is "evaluated" by the PHP parser so in a very miniscule way outputting something in double quotes that doesn't need to be takes longer than using single quotes, unless of course you ARE outputting a variable. Additionally, by keeping your HTML only output in single quotes, you'll be able to read it easier (no slashes). I would suggest this: print '<a class="sidelink" href="http://www.lds.org">LDS Church</a><span class="hide"> | </span>' . "\n"; edit: Oops, forgot to say that also in a very miniscule way, technically echo is faster than print, since echo does not return a result. Quote Link to comment Share on other sites More sharing options...
davidp Posted July 10, 2007 Author Share Posted July 10, 2007 thanks treenode and wildteen. i wasn't aware of those small differences between single-quoted strings and double-quoted strings. that helps a lot. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.