Rick Skeels
Rick Skeels

Reputation: 513

Using php include with in a echo

I need to echo an php include. I have tried the below but neither work?

echo "<td>include'file.php'</td>";
echo "<td><?php include/file.php'?></td>";

Why doesn't this work and how can this be achieved?

Upvotes: 3

Views: 9526

Answers (5)

Nils
Nils

Reputation: 446

ob_start();
include 'file.php';
$output = ob_get_clean();

This way the php will be evaluated and all output will be saved into a variable

Upvotes: 1

TecBrat
TecBrat

Reputation: 3729

If the include file returns something, then do it like this:

echo '<td>'.include('file.php').'</td>';  // highly unlikely though.

The more likely correct thing to do is this:

echo '<td>';
include('file.php');
echo '</td>';  

If the included file is just text, then use file_get_contents().

Upvotes: 1

Austin Brunkhorst
Austin Brunkhorst

Reputation: 21130

You're evaluating everything as a string. You need to do something like this.

echo '<td>', file_get_contents('file.php') ,'</td>';

Upvotes: 1

Mr. Alien
Mr. Alien

Reputation: 157314

You don't have to echo included files, include means you are simply embedding the source code of page1 in page2, if you echo on page1 and you include on page2 and if your page1 has something like

echo 'hello';

This will be simply echoed on page2 when you include page1.

So you need to simply use include 'file.php'; that's it and use echo on file.php

Also, I would like to tell you that am sure you are doing something which is not usually done, like, why you need to include a file inside a just a td tag? Is that file used for some string/int output?

Upvotes: 4

Repox
Repox

Reputation: 15476

You cannot start a new php block inside a php block.

You would probably do something like this:

echo "<td>";
include 'file.php';
echo "</td>";

Upvotes: 9

Related Questions