How to insert php include inside heredoc variable? - php

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;

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;

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 :
php.net - heredoc

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

Related

Echo php with echoed html php

I currently have a php which echo my html template.
However in that HTML template there is another echo which calls from another php script.
Just wondering how do I do that? Because once I echo my html template the other it doesn't seems to echo my content from the other php script.
HTML TEMPLATE
<php? $html = '<span>name:<?php echo $name; ?></span><span>email:<?php echo $email; ?></span>' ?>
CONTACT TEMPLATE
<php? $name = "hello world"; $email = "hello#world.com"; ?>
I can see what you're trying to do, and it's a simple error. You can't escape php like that whilst inside setting a variable.
Also, I must add that you are declaring php incorrectly.
This is preferred
<?php
not
<php?
So make sure for your contact template you use the correct tag.
Also to include a file you have to call it/require it.
Back to the original question - Here is your method
<php? $html = '<span>name:<?php echo $name; ?></span><span>email:<?php echo $email; ?></span>' ?>
Here is the correct method
<?php
require('contact.php');
$html = '<span>name:'.$name.'</span><span>email:'.$email.'</span>';
echo $html;
?>
First I created the variable. And when doing so I insert the existing variables by escaping the php. Only once this final variable is created do I echo it.
Hope this helps you on your way.
Try to use include. The include statement includes and evaluates the specified file, in this case - your template.
Just Concatenation
<?
$html = '<span>name:'.$name.'</span><span>email:'.$email.'</span>';
?>
Change the tags from <php? ?> to <?php ?> in your script

Using PHP as Template Engine and having a thin Template

I'm using PHP as a template engine per this suggestion: https://stackoverflow.com/a/17870094/2081511
I have:
$title = 'My Title';
ob_start();
include('page/to/template.php');
$page = ob_get_clean();
And on page/to/template.php I have:
<?php
echo <<<EOF
<!doctype html>
<html>
<title>{$title}</title>
...
EOF;
?>
I'm trying to remove some of the required syntax from the template pages to make it easier for others to develop their own templates. What I would like to do is retain the variable naming convention of {$variable} but remove these lines from the template file:
<?php
echo <<<EOF
EOF;
?>
I was thinking about putting them on either side of the include statement, but then it would just show me that statement as text instead of including it.
Well, if you want a VERY simple templating solution, this might help
<?php
$title = 'My Title';
// Instead of including, we fetch the contents of the template file.
$contents = file_get_contents('template.php');
// Clone it, as we'll work on it.
$compiled = $contents;
// We want to pluck out all the variable names and discard the braces
preg_match_all('/{\$(\w+)}/', $contents, $matches);
// Loop through all the matches and see if there is a variable set with that name. If so, simply replace the match with the variable value.
foreach ($matches[0] as $index => $tag) {
if (isset(${$matches[1][$index]})) {
$compiled = str_replace($tag, ${$matches[1][$index]}, $compiled);
}
}
echo $compiled;
Template file would look like this
<html> <body> {$title} </body> </html>

PHP & HTML: How to include .php in concatenated PHP string var

Example of what i am trying to do:
<?php
$html_element = '<form><label>country</label><select>'.include_once("country_list.php").'</select></form>';
?>
I have tried the above and the following:
<?php
$html_element = '<form><label>country</label><select><?php include_once("country_list.php"); ?></select></form>';
?>
Of course the "$html_element" is then echoed in some div later. So how can i include the "country_list.php" in this php string variable so that it will pull correctly?
If you want to include the data, no matter which file type it is, do like this:
<?php
$html_element = '<form><label>country</label><select>'.file_get_contents("country_list.php").'</select></form>';
?>

Order of inclusion and interpretation for php and html

I am trying to display different content on a page based on some options.
Also, I am trying to avoid using php echo for all the html output.
I came up with the following solution accidentally, and now I'm confused about how it actually works.
test.php
<?php
function get_content() {
$page = 0;
if($page == 0)
include('page0.php');
else
include('page1.php');
}
?>
<html>
<body>
<?php echo get_content() ?>
</body>
</html>
page0.php
<?php
$link = "http://www.google.ca";
$name = "GOOGLE";
?>
<?= $name ?>
page1.php
<?php
$link = "http://www.yahoo.ca";
$name = "YAHOO";
?>
<?= $name ?>
It seems like the php interpreter would end up including html tags into a <?php ?> block when it reaches the following line, but somehow, this code works, and the outputted html is valid.
include('page0.php');
Can someone explain what exactly is going on here?
When a file is included, parsing drops out of PHP mode and into HTML
mode at the beginning of the target file, and resumes again at the
end. For this reason, any code inside the target file which should be
executed as PHP code must be enclosed within valid PHP start and end
tags.
From PHP manual, include function.

PHP: Breaking out of PHP for HTML but return as value instead of print on page?

I am generating a lot of HTML code via PHP, but I need to store it in a variable, not display it immediately. But I want to be able to break out of PHP so my code isnt a giant string.
for example (but actual code will be much larger):
<?php
$content = '<div>
<span>text</span>
link
</div>';
?>
I want to do something like this:
<?php
$content =
?>
<div>
<span>text</span>
link
</div>
<?php
;
?>
But I know this will not return the html as a value it will just print it to the document.
But is there a way to do this tho?
Thanks!
You can use output buffering:
<?php
ob_start();
?>
<div>
<span>text</span>
link
</div>
<?php
$content = ob_get_clean();
?>
Or a slightly different method is HEREDOC syntax:
<?php
$content = <<<EOT
<div>
<span>text</span>
link
</div>
EOT;
?>
Try this:
<?php
$var = <<<EOD
my long string
EOD;
echo $var;
?>
(edit done, but one has edit faster than me :))
In my opinion, you should look into a template engine such as Smarty. It can help you take some of the ugliness out of hardcoding HTML into the PHP file, and can help make the code more manageable.
You could store the html in an html file and read that file into a variable.
While still technically a string, you might find the documentation on PHP's heredoc to be an entertaining read:
heredoc syntax
$content = <<<EOT
<div>
<span>text</span>
link
</div>
EOT;
Use heredoc syntax.

Categories