7

I need to have include page inside the php heredoc variable but it doesn't work please help me.

$content = <<<EOF
include 'links.php';
EOF;
0

5 Answers 5

19

You can do this way:

ob_start();
include 'links.php';
$include = ob_get_contents();
ob_end_clean();

$content = <<<EOF
{$include}
EOF;
Sign up to request clarification or add additional context in comments.

1 Comment

You could combine ob_get_contents and ob_end_clean to ob_get_clean :)
3

Simple: you cannot do it. You can include the file before hand, store it in a variable, then insert it into the file. For example:

$links_contents = file_get_contents('links.php');
//$links_contents = eval($links_contents); // if you need to execute PHP inside of the file
$content = <<<EOF
{$links_contents}
EOF;

2 Comments

This is going to include the source of links.php, not the executed content.
Don't eval after running file_get_contents. It won't work the way you're expecting. The reason is that include (and hence the links.php file) start with the PHP interpreter turned off. That's why you need <?php to turn it on (It starts in non-code context). eval starts with the interpreter turned on (you don't need to prefix php code with <?php to get it to work). So it won't work the way you expect it to. Not to mention the other evils of eval... So -1 for what boils down to bad advice that won't work...
2

What do you mean by not working? As in contents of 'links.php' isn't in $content? If thats what you want try using output stream redirection (or just read the file).

<?php
ob_start();
include 'links.php';
$content = ob_get_contents();
ob_end_clean();

echo "contents=[$content]\n";
?>

1 Comment

sigh every time I answer a question with no answers, there are 3 or 4 answers before I can finish typing.
1

Heredoc syntax is made to handle text only. You can't include a file or execute php methods in it.


Resources :

Comments

1

Do not use heredoc at all.
if you need to output your contents - just output it as is, without storing it in the variable.
There can be very limited use of output buffering and I am sure here is not such case.

Just prepare your data and then output it using plain HTML and PHP.
make your pages like this (right from the other recent answer):

news.php:

<?
include "config.php"; //connect to database HERE.
$data = getdbdata("SELECT * FROM news where id = %d",$_GET['id']);
$page_title = $data['title'];
$body = nl2br($data['body']);

$tpl_file = "tpl.news.php";
include "template.php";
?>

template.php:

<html>
<head>
<title><?=$page_title?></title>
</head>
<body>
<? include $tpl_file?>
</body>

tpl.news.php

<h1><?=$page_title?></h1>
<?=$body?>
<? include "links.php"  /*include your links anywhere you wish*/?>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.