views:

364

answers:

4

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.

+1  A: 

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.

erenon
+5  A: 

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
Derek Illchuk
very nice simple explanation and illustration. I was completely lost as to what he was saying.
stephenbayer
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 file
Richard
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*???
Jason Rhodes
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)
Richard
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.
Jason Rhodes
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 = off
Richard
A: 

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.

Jason Rhodes
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...
Richard
+1  A: 

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
Alex