• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

php annoy = 1; ( newline incorrectly ignored...grrr)

MrColin

Platinum Member
I'm teaching myself php (or at least trying) from various resources, however (as is always the case with this sort of approach) my environment behaves differently than any example/book/tutorial I can find.

I've got a LAMP server that works great for some packaged apps already however trying out some examples i find my output is different than sources suggest.

using this example:
Code:
<?php

if ($handle = opendir('/path/to/files')) {
    echo "Directory handle: $handle\n";
    echo "Entries:\n";

    /* This is the correct way to loop over the directory. */
    while (false !== ($entry = readdir($handle))) {
        echo "$entry\n";
    }

    /* This is the WRONG way to loop over the directory. */
    while ($entry = readdir($handle)) {
        echo "$entry\n";
    }

    closedir($handle);
}
?>

This should give output on separate lines, but for me it is all on one line. Using google I can find all kinds of stuff related to removing unwanted newlines, but nothing on why newlines are inexplicably ignored.

Any ideas?
 
nevermind, I think I figured it out, \n is for playing on the console, while <br> is what I needed to use instead.
 
If you're outputting html, correct, /n is just whitespace that will be ignored. Ultimately you should avoid <br> as well. Use <p> tags to denote paragraphs for block text output, or in a case like this dump each line into a separate <div>.
 
If you're outputting html, correct, /n is just whitespace that will be ignored. Ultimately you should avoid <br> as well. Use <p> tags to denote paragraphs for block text output, or in a case like this dump each line into a separate <div>.

Correct

or if you are really lazy like me, dump it into a <pre> block 😛
 
Back
Top