1

If I have a piece of code that reads a chunk of HTML from a txt file and then echos that html onto the page, how can I accomplish the same task, but when there is PHP inside of the txt file?

ex:

this is the file being read:

<?php 
$filecontent = // read some other file
echo($filecontent);
?>

and this is the page that is reading the file:

<?php
$code1 = //reading the above file
?>
<html>
<?php echo($code1); ?>
</html>
7
  • 1
    echo is a language construct and does not require parentheses. Commented Jul 11, 2013 at 15:56
  • @JasonMcCreary well when i started learning, the guy i was watching did it, and its just become a habit. It works nonetheless Commented Jul 11, 2013 at 15:57
  • I would recommend using an include statement instead. Commented Jul 11, 2013 at 15:58
  • Just cause the guy did it doesn't make it right. Too many bad habits in the PHP community. Help clean it up. Commented Jul 11, 2013 at 15:58
  • 2
    @JasonMcCreary: This is a religious argument. Commented Jul 11, 2013 at 16:01

4 Answers 4

2

When you want to process files containing PHP code you need to use include instead of echo.

<?php include('your_php_file_name'); ?>

If you have the contents of the file in a string you are in a tough spot because the only way to process the code is eval, and in addition you have to properly set up any environment that the code requires. eval itself should be avoided, and the latter is impossible to do in the general case.

Sign up to request clarification or add additional context in comments.

4 Comments

So why did you echo'ed it?
@ElmoVanKielmo: Mistakes happen :-)
if there is html in addition to php in the file that is being read, how does that get handled?
@nife552: Exactly the way it's handled in the file doing the reading -- inside PHP tags code gets executed, outside them stuff is printed verbatim.
0

Use include instead of echo:

<?php include($file_that_contains_php); ?>

2 Comments

include is a language construct and does not require parentheses.
Does not require, but it can be used. I always use it, mostly for readability.
0

you need to include the first file and echo statement in the first file will get executed.

<html>
<?php require_once("firstfile.php"); ?>

Comments

0

You need to echo htmlentities($code1), because when you echo then browser will not show it contents, because it try to parse it as a html tag, but htmlentities will encode to safe html output this characters. If you want to evaulate the code, then you need eval($code1) or include it.

Comments

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.