Jump to content
xisto Community
mrdee

Weird Formatting: Embedding Php Into Html

Recommended Posts

I have a funny problem when I embed PHP in a HTML page
whenever I write some PHP code with the 'print' or 'echo' command, the output shows the end bits of the PHP code, eg. when I write:

<?phpprint "This is PHP";?>
the output I get is:

This is PHP";?>

So, the final quote mark, the semicolon and the ending PHP tags are displayed.
Has anyone else come across this problem before, and does anyone know a solution for it?

By the way, the full code would be:
<html><head></head><body>Some HTML<?phpprint "This is PHP";?></body></html>
The funny thing is, this does not happen when I do a PHP only page.

All help will be appreciated.

Share this post


Link to post
Share on other sites

Is the file saved as a php or an html file type? If it is an html file extension, the php is not being parsed properly.

Share this post


Link to post
Share on other sites

Right, I see.The file is indeed saved as a .htm file.When I save as a .php file, the problem is indeed not there.However, sometimes PHP is embedded in a HTML page, and that usually works.So, what do I do then if I ever have to include a piece of PHP into a HTML page?Do I just put a link to a PHP page?Or is there another way to make the server parse the PHP correctly?I seem to remember I have done HTML pages in the past, and there was some PHP code inside the HTML and it worked.All this is not really good for my poor head, confusion all around. :)

Share this post


Link to post
Share on other sites

However, sometimes PHP is embedded in a HTML page, and that usually works.So, what do I do then if I ever have to include a piece of PHP into a HTML page?
Do I just put a link to a PHP page?
Or is there another way to make the server parse the PHP correctly?


When you embed php code into the html file you should save the file with .php extension. If the file is saved as html, the browser simply ingnores everything in between <?php ?> tags and process the rest of the page. Because, it doesn't know these tags. Thats it. When the server process the php file, it process the php elemetns and generates the output simply as html. That's why you won't see any php code when you view the source of the page in browser.

I think this is your problem. If not, please more clear.....Actually I didn't understand your problem clearly.

Share this post


Link to post
Share on other sites

OK, thanks for your explanations, gentlemen.I think I have got it now.I assume, if I embed PHP into a HTML file and then save it as a .php page, the server will parse and execute the PHP correctly, but it will also show the HTML code that is in the page correctly.Is that right?Thanks for the help, once again.

Share this post


Link to post
Share on other sites

I assume, if I embed PHP into a HTML file and then save it as a .php page, the server will parse and execute the PHP correctly, but it will also show the HTML code that is in the page correctly.

Is that right?


Yes. Actually the server only parses the php code only. It has nothing to do with html directly. Even when you echo or print in php, that output is html. For example try

echo "Hello <br /> There";

you'll see Hello in one line and There in another line. It is because, the web server treats <br /> tag in html

as html tag and simply ingnores it. The html will be rendered by the browser itself. Your web browser renders all this html code and show you the page. No involvement of web server in this. That is php is processed by Web Server and HTML is processed by your browser.

Share this post


Link to post
Share on other sites

Check the mime type configuration on your account, and see if "htm" is set to be considered as a PHP file.

Share this post


Link to post
Share on other sites

What you need is to tell the server to parse HTML pages as PHP pages. Add this code:

AddType application/x-httpd-php .html

(remember to change .html to .htm is thats the extension you use.)

to a file named .htaccess (no filename, simply the extension .htaccess ) If on windows name the file something.htaccess and then rename it manually to remove the part before the dot as windows is a fussy character...

Then just upload the file to your root directory on your server and that should help. (thats also how they create dynamic images like site.com/image.png?name=shadowx but they use .png instead of .html)

Share this post


Link to post
Share on other sites

As good practice, I name all of my files with a .php extension. It is just a more effective way of naming files, especially since I am a heavy PHP user. I have never used the .htm extension, only .php and .html for files like that. I would suggest to just name all of your files with HTML and/or PHP with a .php extension for simplicity's sake.

Share this post


Link to post
Share on other sites

if you want to integrate PHP with HTML you should save with .php extension and you should follow the following code:

<?php//starts phpecho 'This is a php script';//ends php?>/* this should be on one file  */<html><body>This is a HTML</body></html>

Share this post


Link to post
Share on other sites
How to embed any rich text editor in a php pageWeird Formatting: Embedding Php Into Html

I want to use a rich text editor in my php page. Please tell me how to embed/ include that rich text editor so that it will save the content and show it.

Thanks

-reply by shubhaanshu

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.