Include Function, New Line

When using the php include function, how can I display html code on a new line/line break?
For ex:

HTML CODE
<?php include("file.php");?>
HTML CODE

Will print
HTML CODE<?php include("file.php");?>HTMLCODE

Any way to fix this? I know I can add additional line breaks in the php file itself, but in practice of professional programming(everyone does it in their own way), what’s the best way? I would like to see multiple solutions.

An actual example from my code

<!DOCTYPE html>
<html>
<head>
	<title>PAGE - TITLE</title>
</head>
<body>
<div class="page-content">
About us page<br></div></body>
</html>

It’s not clear from your example what’s in the actual include file, but you shouldn’t get the result you are suggesting—unless perhaps the “HTML code” is not actually code but just text.

Do you mean that when you view the source code in your browser, you want the code to be properly formatted with proper line breaks? If that’s the case, use \n where you want the code to break to a new line.

1 Like

Yes I know I can use \n but it will not work on <?php include("file.php"); ?>

<?php include("header.php"); ?>
<?php include("content.php"); ?>
<?php include("footer.php"); ?>

You will have to do this

<?php include("header.php"); ?>

<?php include("content.php"); ?>

<?php include("footer.php"); ?>'

This would work?

<?php 
include("header.php"); 

include("content.php");

include("footer.php");
?>

Sorry for being to vague.

<?php
echo PHP_EOL;
include("content.php");
?>

That will output a single line break before the included content.

1 Like

Thank you!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.