Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 965
Richard Avatar asked Dec 15 '09 15:12

Richard


People also ask

How do you make a new line in PHP?

Answer: Use the Newline Characters ' \n ' or ' \r\n ' You can use the PHP newline characters \n or \r\n to create a new line inside the source code. However, if you want the line breaks to be visible in the browser too, you can use the PHP nl2br() function which inserts HTML line breaks before all newlines in a string.

What is the purpose of <? PHP ?> tags?

The tags tell the web server to treat everything inside the tags as PHP code to run. The code is very simple. It uses an in-build PHP function "echo" to display the text "Hello World ..." in the web page.

Do I need to close <? PHP?

To recap, closing PHP tags (?>) are entirely optional if you're coding a PHP-only file and there's no intermingled HTML. Files containing single class definitions are good candidates. Omitting the closing tag has another benefit: it becomes impossible to accidentally add white space to the end of the file.

How do you close a PHP tag in HTML?

The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block. The closing tag for the block will include the immediately trailing newline if one is present. <?


2 Answers

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 
like image 138
Derek Illchuk Avatar answered Oct 03 '22 15:10

Derek Illchuk


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

like image 44
atw527 Avatar answered Oct 03 '22 15:10

atw527