-4

I have an html file called main.html

main.html

<!DOCTYPE html>

<!--PHP goes here-->

</html>

And a PHP file called hello.php

hello.php

<?php

echo "<p>Hello!</p>";

?>

I want the html file to open the php file, and put it into the webpage so that i get this...

<!DOCTYPE html>

<p>Hello!</p>

</html>

What is the literal code to do this?

NOTE: I'm trying to get the actual PHP file in there. I DO NOT want this ...

<!DOCTYPE html>

<!--I already know how to do this!-->
<?php echo "<p>Hello!</p> ?>

</html>

Thanks

NOTE: main.html is an actual html file on a physical server. I've just simplified it.

6
  • php.net/manual/en/function.include.php Commented May 16, 2014 at 2:21
  • You can't run server-side code in a plain HTML file (except with SSI) Commented May 16, 2014 at 2:22
  • well actually you can't embedd the php into html you have to embedd html into php . and that can be done using include or require. In HTML you can get the output using javascript(i.e. with the help of Ajax) Commented May 16, 2014 at 2:23
  • This is an html file on a physical server. I've just simplified it. Commented May 16, 2014 at 2:23
  • jQuery.load() ? Commented May 16, 2014 at 2:37

3 Answers 3

0
<?php include 'hello.php'; ?>

To meet the minimum word requirement, I may as well say that "include" literally just includes a file in the page, and if it is php, it is then parsed too.

Oh, also, it's been a long time since I've done any web programming. Change the file extension from .html to .php! (I wouldn't have caught that without someone else mentioning it)

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

3 Comments

It should be noted that unless you modify your server configuration, you will NOT be able to execute PHP code in a .html file
how can you include a php file into HTML it's not gonna be parse.
Yeah, sorry guys, been a long time, and the moment I noticed, I had to rush off and do something else (school stuff)
0

An HTML file cannot include PHP. You would need to rename your main.html file to main.php and then include/require the file. Also you'll want to open the HTML tag after you declare the doctype (since you closed it at the end) :)

<!DOCTYPE html>
<html>

<?php
include "hello.php";
?>

</html>

If you want information on the differences between include/require/include_once/require_once see here: Difference between require, include and require_once?

2 Comments

If i just rename main.html to main.php will the HTML code execute properly?
Yes, HTML code will always execute properly. If you keep it as HTML it will simply output your PHP code for everyone to see. If it is named as .php the web server will execute the PHP.
-1

You could use jQuery...

<!DOCTYPE html>

<head>
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script>
    jQuery(document).ready(function(){
        jQuery("#phpPage").html('<object data="hello.php">');
    });
</script>
</head>

<body>

<p>Hello!</p>
<div id="phpPage"><\div>

</body>
</html>

1 Comment

The down vote is interesting, seeing as I'm the only one who provided a solution that doesn't involve PHP as the OP asked. This is how you include a PHP page in an HTML page.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.