Is there a way to force a new line after php closing tag ?> when embedded among html?
Asked Answered
L

8

23

I have been searching online with little success for a way to force php to output a newline after a closing php tag ?> so that my HTML will be rendered as I see it before its parsed by PHP so that it will look neat and tidy when viewing the html source code for a page.

I want to stop it from removing the newline and causing the next line from wrapping up and therefore ruining the format of the page. Is there a specific php.ini setting I can change to disable this behaviour?

All my code logic files have ?> removed from the end of them so I wont need to worry about them injecting extra newlines which is why I believe PHP was coded to strip the trailing new lines.

Latchet answered 15/12, 2009 at 15:24 Comment(4)
Can you post quick example of exactly what you're referring to?Protection
w...t...? HTML ignores new lines and any white space. I'm not sure what your saying exactly, but if you want text on a new line, you'll have to use standard html formating, like placing the content in a <div> or <p> or at least adding a <br /> afterwards.Holmium
sorry if my description was unclear. My issue is not with the browser rendering content on a new line but rather than when you view the page source the new lines are not there. As per Derek's code sample. The HTML is totally fine/passes all standard's validation checks. I just like it when my code is neatly indented etc...which PHP's closing tag kinda spoils.Latchet
Thanks for asking this question; I actually ran into exactly the same issue with PHP removing newlines after the closing ?> tag -- and unlike in html, when working with mail() headers stripping a newline MATTERS.Erv
F
18

Richard is saying this:

Hello <? ?>
Jello

...after PHP-parsing becomes this:

 Hello Jello


I just add an extra newline manually, like this:

Hello <? ?>

Jello

...giving this:

Hello
Jello
Frendel answered 15/12, 2009 at 15:34 Comment(6)
very nice simple explanation and illustration. I was completely lost as to what he was saying.Holmium
I guess I'll just leave it in that case. I don't want extra line spaces all over my page templates. I just wanted to check this behaviour of PHP can't be circumvented some how in the config fileLatchet
I'm still pretty unclear about what you want. I've never had <div>...</div> <?php ... ?> <p>...</p> turn into <div>...</div> <p>...</p>. It will render the source as you type it, so with one line break there will be a line break, with none, there will be none. Scratches head???Launderette
if you write some neatly formatted html for say a table <table><tr><td><?php echo $foo; ?></td></tr></table> in a php file and you put the table, tr, td and <?php tags on new lines (each one more indented than the other and then visit the page and view the source code (not the output in the browser) the closing </td> line will wrap onto the line on which your php variable was echoed. Therefore your </td> is not neatly aligned with the opening <td> tag as it was when you where coding it (and the PHP parser has not processed it)Latchet
Ah ok I see what you're saying. You could change <?php echo $foo; ?> in your example to <?php echo $foo; \n ?> and solve your problem, but that might be a lot of work.Launderette
Yeh, to much work really...I was hoping it might be as simple as a php.ini setting like remove_new_lines_after_closing_tag = offLatchet
P
9

This is an old question, but to keep the new line, simply place a space after the closing tag.

Source: http://php.net/manual/en/language.basic-syntax.instruction-separation.php

Propraetor answered 27/7, 2013 at 23:7 Comment(3)
this doesn't answer the questionXenos
This does work without problems but feels a little hacky. Is this documented properly anywhere?Hood
Forgot to mention that the reference is in the user comments in the above link. That is the most concrete documentation I've found on the feature.Propraetor
A
2

There is no way to change this behavior. To make it clear what's happening, I always explicitly put the newline character in my template files.

Hello,    

Foo: <?php echo $foo."\n"; ?>
Bar: <?php echo $foo."\n"; ?>

This concludes foo and bar.

Outputs:

Hello,    

Foo: foo
Bar: bar

This concludes foo and bar.
Autarchy answered 16/5, 2013 at 11:22 Comment(0)
F
1

You can try this:

echo "output \n";

or even this much less elegant technique:

echo "output
";

Please paste some example code snippet to get more specific help.

Fonseca answered 15/12, 2009 at 15:28 Comment(0)
E
0

You can't always just start a block level element after the php tag.

Sometimes, you are generating plain text for inside a textarea.

Other times, you are not generating HTML strings at all.

  • SQL statement
  • email body
  • other non-HTML strings
Erin answered 8/2, 2010 at 14:40 Comment(1)
this is wrong. the op is not asking for an extra newline to be added, he is asking for existing newlines in the source to be preserved (the current behaviour of php is to remove an existing newline during execution)Underarm
D
0

My compulsive habits drive me to similar goals. Sadly, there is no elegant solution for this. Two suggestions:

  1. Output a newline character where you want to break the HTML code like so:

    echo 'Hello World'.chr(10);

  2. PHP's 'echo' command is not an actual function so you cannot override it; however, you could create a wrapper and use that instead:

    function myecho($string, $newline=true)
    {
       echo $string;
       if($newline)
          echo chr(10);
    }
    

Neither solution is elegant, but then again, neither is the HTML output if you don't add line breaks.

Danelledanete answered 14/5, 2013 at 2:36 Comment(0)
B
0

You can make this slightly more elegant:

<?=$var.PHP_EOL?>

But in the end this is just a missing feature: If you're not in HTML/XML-context, just want to output simple, pure text you WANT to output the text "as is" and not have magically stripped the newlines away.

Bryonbryony answered 23/5, 2022 at 13:19 Comment(0)
L
-5

If this is causing a problem for you, you're most likely not writing good HTML. If you have PHP separated out from the HTML, then after the ?> close tag, just start with a block level HTML element.

Like this:

Hello <?php $php_code(); ?> <div>Something else here</div> or even <p>Something else here</p>

Which would output:

Hello

Something else here

If instead you're writing:

Hello <?php $php_code(); ?> Something else here

Then your problem is in how you're writing HTML, not PHP parsing.

Launderette answered 15/12, 2009 at 15:38 Comment(1)
It having the source code coming out like the first code sample you posted I am trying to avoid. I want(ed) to have the div and p tags on new lines and correctly indented depending on what level in the DOM they appear. ie. a <head> being indented 1 tab in below a <html> tag etc...Latchet

© 2022 - 2024 — McMap. All rights reserved.