1

I'm having problems embedding php inside an html file.

I first ran in to this problem when I was trying to 'include' a php file inside tags, and thought it was related to css formatting, or something. But now I've broken this down into the simplest php and html possible, with an example from a book that should work, and I'm still getting this problem. Here's the html sample that doesn't work:

<HEAD>
<TITLE>PHP inside HTML tester</TITLE>
</HEAD>
<BODY>

<?php
    echo "Hello World";
?>

</BODY>
</HTML>

I'm expecting 'Hello World' to show up in my browsers, but nothing is displayed. When I try to 'view source', I see exactly the text above. I figure that after all the examples of this I've tried, the code is ok, but something is keeping what's inside the from being recognized.

Any suggestions? And thanks for helping me out with what's probably a dumb question.

5
  • Is there supposed to be a link to some sample code? Show us some code, so we can see what you are trying to do... Commented Sep 29, 2010 at 12:44
  • What is the filename of the file? and does your server have PHP enabled? Commented Sep 29, 2010 at 12:44
  • The code I pasted above is called "website_03.html". And other php scripts I've written work fine, so it appears that the server has php installed. Commented Sep 29, 2010 at 12:57
  • 3
    Call it website_03.php and it will work :-) Commented Sep 29, 2010 at 12:59
  • captaintokyo - genius! it works. Commented Sep 29, 2010 at 13:04

2 Answers 2

7

There is something wrong with your PHP installation. The web server isn't passing requests for PHP pages off to the PHP interpreter.

If you did indeed save the file as an .html file, then your PHP code will never execute because most web servers have their handler mappings set to route only PHP (.php, .phtml, or .inc extensions) files to the PHP interpreter.

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

6 Comments

So since all my files with a .php extension work, but this example doesn't, it does look like the server isn't executing my php script because it's in an html file, and you're correct.
I WAS hoping to use an html framework with all kinds of <div>'s, embedded with php code to construct a web application... but now I've got to move to Plan B. Any ideas what a Plan B would be?
Jacob - thanks for your help. captaintokyo above said to rename the file .php, which is my Plan B, and everything works now.
Don't forget to accept the correct answer (this one!!), so Jacob gets his rightful dose of Reputation points for taking the time to answer your question.
@Reg H, Oops, I'm sorry. I didn't mean to say that.
|
1

Looks like your server is not able to handle php or your server does not know how to handle the file type with - this code is in.

1 Comment

Changing the file name from "website_03.html" to "website_03.php" fixed the problem. Thanks to captaintokyo for that one.

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.