1

My index.php looks like this:

<!DOCTYPE html>
<html>
<head>
<title><?php echo $title?></title>
</head>
<body>

<p>Hi, this is my homepage. I'll include some text below.</p>

<?php
include "myfile.php";
?>
</body>
</html>

"myfile.php" e.g. includes the following:

<?php
$title = "Hi, I'm the title tag…";
?>
<p>Here's some content on the site I included!</p>

The <p> gots outputted but the <title> tag stays blank.

How can I reach to get $title from the included site and also show the included content inside the <body> tag?

7
  • you use echo $title to show in screen. Commented Sep 19, 2016 at 7:29
  • 1
    What were you trying to do. include is used to include files. Make your question a bit more clear. Commented Sep 19, 2016 at 7:29
  • Your file had to included before using the $title variable. Commented Sep 19, 2016 at 7:31
  • @Sasikumar, updated my question. Yes, but I need to include it inside the <body> tag, and not before title tag. How to do that? Commented Sep 19, 2016 at 7:33
  • 1
    You probably can … sort of … by doing some nasty messing about with output buffering then editing the buffer before releasing it to the browser … but the real solution to this problem is to better separate your data from your templates (the MVC pattern is a good one to look at). Commented Sep 19, 2016 at 7:52

2 Answers 2

1

Here is my solution:

index.php

<?php
// Turn on the output buffering so that nothing is printed when we include myfile.php
ob_start();

// The output from this line now go to the buffer
include "myfile.php"; 

// Get the content in buffer and turn off the output buffering
$body_content = ob_get_contents();    

ob_end_clean();
?>

<!DOCTYPE html>
<html>
<head>
<title><?php echo $title?></title>
</head>
<body>

<p>Hi, this is my homepage. I'll include some text below.</p>
<?php
echo $body_content; // Now, we have the output of myfile.php in a variable, what on earth will prevent us from echoing it?
?>
</body>
</html>

myfile.php is unchanged.

P.S: As Noman suggested, you can first include 'myfile.php'; at the beginning of index.php, then echo $content; inside body tag, and change myfile.php into something like this:

<?php
$title = "Hi, I'm the title tag"
ob_start();
?>

<p>Here's some content on the site I included!</p>

<?php
$content = ob_get_contents();
ob_end_clean();
?>
Sign up to request clarification or add additional context in comments.

4 Comments

Your own solution works. But, how does it work. Can you explain that to me, please?
When you include myfile.php, the html part is outputted. I use ob_* function to capture it into a variable instead of letting it be printed out, that's all. ob_start() to turn the output buffering on, ob_get_contents() to get the string stored in buffer, ob_end_clean() to clean the output buffer then turn off the output buffering.
Thanks for the explanation. Have a nice day!
This is an example. Hope this help. P.S: Oh, didn't notice your comment.
0

There is another way to do it:

index.php

<?php
include "myfile.php";
?>

<!DOCTYPE html>
<html>
<head>
<title><?php echo $title?></title>
</head>
<body>

<p><?php $content; ?></p>

</body>
</html>

myfile.php

<?php
$title = "Hi, I'm the title tag…";
$content = "Here's some content on the site I included!";
?>

8 Comments

Don't really understand what you mean. Could you embed this in my code, please!?
You mean to say that you want to show <p>Here's some content on the site I included!</p> into body ? right ?
Yes, that is exactly what I mean.
You should have to do just like this $con = "<p>Here's some content on the site I included!</p>"; and then echo this into a body. This is the only way.
You mean: I'll include two variables, $title and $content. echo $title; is in the html title tag and echo $content; is inside the html body. Right?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.