PHP - 如何加载HTML文件? [重复]

This question already has an answer here:

At the moment I have a file like this

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    echo "<html>My HTML Code</html>";
}
?>

But I wanted to do something like this to keep my php file short and clean.

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    //print the code from ..html/myFile.html
}
?>

How can I achieve this?

</div>

you may have a look at PHP Simple HTML DOM Parser, seems a good idea for your needs! Example:

// Create a DOM object from a string
$html = str_get_html('<html><body>Hello!</body></html>');

// Create a DOM object from a URL
$html = file_get_html('http://www.google.com/');

// Create a DOM object from a HTML file
$html = file_get_html('test.htm');

save your html content as seperate template and simply include it

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    include ("your_file.html");
}
?>

OR

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    readfile("your_file.html");
}
?>

readfile is faster and less memory intensive than file_get_contents

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    echo file_get_contents("your_file.html");
}
?>

This should do the trick

Or, as nauphal's answer say, simply use include()

Don't forget that, if file doesn't exists, you could have some trouble (so, maybe, check before include or getting content)

I think you want to include your HTML file or have I misunderstood the question.

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    include ("..html/myFile.html");
}
?>

Use this code

if(some condition)
{
    //Dont allow access
}
else
{
    echo file_get_contents("your_file.html");
}

OR

if(some condition)
{
    //Dont allow access
}
else
{
    require_once("your_file.html");
}

Extending nauphal's answer for a more robust solution..

<?php
if(some condition)
{
    //Dont allow access
}
else
{
    if(file_exists("your_file.html"))
    {
       include "your_file.html";
    }
    else
    {
      echo 'Opps! File not found. Please check the path again';
    }
}
?>

Use functions like

include()
include_once()
require()
require_once()
file_get_contents()

Way 1:

ob_start();
include "yourfile.html";
$return = ob_get_contents();
ob_clean();

echo $return;

Way 2: Use templaters, like CTPP, Smarty, etc... Templaters are useful to transfer some logic from php to template, for example, in CTPP:

$Templater -> params('ok' => true);
$Template -> output('template.html');

in template html:

<TMPL_if (ok) >
ok is true
<TMPL_else>
ok not true
</TMPL_if>

The same ideas are in other templaters. Templaters are better, cause it helps you to standartize your templates and send all primitive logic to them.