How to include php in heredoc variable?

I need to include the page inside the php heredoc variable, but it does not work, please help me.

$content = <<<EOF
include 'links.php';
EOF;
+3
source share
5 answers

You can do this:

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

$content = <<<EOF
{$include}
EOF;
+14
source

Simple: you cannot do this. You can include the file before hand, save it in a variable, and then paste it into the file. For instance:

$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
source

, ? "links.php" $content? , , ( ).

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

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

heredoc .
- , .
, , .

, , HTML PHP.
( ):

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*/?>
+1

Heredoc . php- .


:

0

Source: https://habr.com/ru/post/1765174/


All Articles