Jump to content

[SOLVED] newlines not working


davidp

Recommended Posts

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&section=0">Projects</a><span class="hide"> | </span>';
    print '<a class="sidelink" href="index.php?page=1&section=1">Source Code</a><span class="hide"> | </span>';
    print '<a class="sidelink" href="index.php?page=1&section=2">Contest Problems</a><span class="hide"> | </span>';
    print '<a class="sidelink" href="index.php?page=1&section=3">Tutorials</a><span class="hide"> | </span>';
    print '<a class="sidelink" href="index.php?page=1&section=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?

Link to comment
Share on other sites

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";

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.