3

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?

8
  • 6
    stop down voting questions just because it is trivial. people need to learn from some where. Commented Aug 19, 2013 at 13:00
  • 3
    I can see how this question may appear a good one to newbies. Don't downvote it. Commented Aug 19, 2013 at 13:01
  • 2
    Could you read the tooltip that comes with downvote: "This question does not show any research effort". I'm not sure how this question demonstrates any form of research effort. The manual page ( php.net/manual/en/function.include.php ) clearly shows an example that is completely NOT like this code. ( It even has an example echo-ing a string, then including something, then echo-ing again) Commented Aug 19, 2013 at 13:02
  • 1
    @RickNash: Just relax, and think about this for a second: you're using echo, so you're already working "in PHP mode". Why, then, would you need to use the opening <?php tag again? why wouldn't you just use an include statement, and echo any markup as a string, no PHP in the strings at all. That would make your code more readable, wouldn't it? Commented Aug 19, 2013 at 13:10
  • 1
    @DevZer0: that's not an argument and/or response to my argument. It does come quite close to an ad-hominem though? @ rick: no worries, it is not a personal thing, it is just the way this site works: to keep people interested, there is this voting thing trying to get the interesting questions more attention than other questions. Commented Aug 19, 2013 at 13:11

5 Answers 5

9

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>";
Sign up to request clarification or add additional context in comments.

Comments

4

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?

1 Comment

@Jimbo muhahaha :p thnx
1

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

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

Comments

1

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().

Comments

1
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

2 Comments

Why make it more complicated? The OP is just getting their head around strings. One thing at a time.
The question was "Using php include with in a echo". If someone later on wants to know what the actual answer is to that, here it is.

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.