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;
You can do this way:
ob_start();
include 'links.php';
$include = ob_get_contents();
ob_end_clean();
$content = <<<EOF
{$include}
EOF;
ob_get_contents and ob_end_clean to ob_get_clean :)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;
links.php, not the executed content.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...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"; ?>
Heredoc syntax is made to handle text only. You can't include a file or execute php methods in it.
Resources :
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*/?>