Jump to content

Renaming HTML file as PHP file


TomTees

Recommended Posts

I have a file ("header.html") that predominately has HTML in it.

 

This may sound silly, but I would like to rename it as a .php file so that when I add comments next to my PHP, my IDE will shade them gray.

 

I just changed all of my PHP comments from

 

// Some PHP comment

 

to

 

<!-- New and Improved PHP comment -->

 

only to realize that PHP freaks out and doesn't treat them as comments - like NetBeans falsely does - but treats them as erroneous code.

 

(Now I have to go back and fix all of my files back to the way they were?!)  >:(

 

If I could change my file from "header.html" to "header.php" then all of my // Comments would be grayed out in my PHP blocks and make my code easier to read.

 

To achieve that end, is it okay to rename a file from .html to .php even if most of the content is HTML and only a little is PHP?

 

(BTW, could you have a file that has 100% html content saved as a .php file and still have it work properly?!)

 

Thanks,

 

 

TomTees

 

Link to comment
Share on other sites

You can call the file html htm or php and it will still act the same on the web. The only parts that matter are the coding of the page.

 

Also to comment in php you would use

<?php */ Comment */ ?>

 

That works only in between the  php start and ends. The normal html comments with the

<!-- -->

will work fine outside of the php tags even if on a php page.

 

However if your doing all this just so you can read your code easier, I have no comment to that. Its just kinda well.... ya no comment.

 

Link to comment
Share on other sites

It is part of your question, since apparently you have them enclosed in php tags without echoing them into the html of the page. If they are intended as HTML comments, they need to be echoed like all of the other HTML. They cannot be used as php comments. If you rename .html files to .php extensions, the only thing that will be interpreted by php is what is inside <?php ?> tags. Everything else is ignored, and sent directly as output to the browser.

Link to comment
Share on other sites

You can call the file html htm or php and it will still act the same on the web. The only parts that matter are the coding of the page.

 

Okay, but to be more correct - as I found out last week - you can have 100% HTML in a PHP file and it ill run okay, but if you have any PHP in an HTML (by your webserver default) the PHP won't run.

 

Right?

 

 

Also to comment in php you would use

<?php */ Comment */ ?>

 

That works only in between the  php start and ends. The normal html comments with the

<!-- -->

will work fine outside of the php tags even if on a php page.

 

Same if it is an HTML file.

 

That is where I screwed up.  (Of course it didn't help that NetBeans tricked my into thinking I could do this...

 

<?php
<!-- Comment grayed out in NetBeans but invalid when code runs!! -->
?>

 

 

However if your doing all this just so you can read your code easier, I have no comment to that. Its just kinda well.... ya no comment.

 

Yeah, GOD FORBID someone actually tried to make their code WELL-DOCUMENTED and READABLE!!  (What w-a-s I thinking?!)  ::)

 

 

 

TomTees

 

 

Link to comment
Share on other sites

Okay, but to be more correct - as I found out last week - you can have 100% HTML in a PHP file and it ill run okay, but if you have any PHP in an HTML (by your webserver default) the PHP won't run.

 

Right?

 

Yes. You can edit your .htaccess file to runn php on a html/htm page, but that's for another day.

 

If your have your answer now you can mark this post solved.

Link to comment
Share on other sites

Okay, but to be more correct - as I found out last week - you can have 100% HTML in a PHP file and it ill run okay, but if you have any PHP in an HTML (by your webserver default) the PHP won't run.

 

Right?

 

TomTees

 

Not exactly. In the default configuration, that's correct. But additional file extensions can be specified to be parsed by the php interpreter in .htaccess or (if you have access to it) httpd.conf.

Link to comment
Share on other sites

It is part of your question, since apparently you have them enclosed in php tags without echoing them into the html of the page. If they are intended as HTML comments, they need to be echoed like all of the other HTML. They cannot be used as php comments. If you rename .html files to .php extensions, the only thing that will be interpreted by php is what is inside <?php ?> tags. Everything else is ignored, and sent directly as output to the browser.

 

So when you are building a website, is there any logical reason to name a file as HTML?

 

(It seems to me that it would be easier to name HTML, HTML+PHP, and PHP files just as .php for simplicity?!)

 

 

TomTees

 

 

Link to comment
Share on other sites

It's all good Pikachu. I'm a self taught newb. I'm not arguing with your responses...lol

 

As far as naming files, I never use html anymore. It's just htm or php. If your aren't going to have php commands on a page then why name it php? You can but that's up to you. There is nothing wrong with having htm pages and then only naming the pages you have php on with the php extension. 99% of the people never never notice it, and 98% of the people going to your site don't know what an address bar is to find it anyways...lol

Link to comment
Share on other sites

For me, if a page has PHP code in it, it's a .php file.  Makes things far easier that way.

 

Keep in mind that regardless of file extension you use, your current context determines which comments will work.  You can't expect HTML comments to work correctly in a PHP context, nor PHP comments to work correctly in an HTML context.

Link to comment
Share on other sites

 

To achieve that end, is it okay to rename a file from .html to .php even if most of the content is HTML and only a little is PHP?

 

(BTW, could you have a file that has 100% html content saved as a .php file and still have it work properly?!)

 

These are essentially the same question.

 

Yes, it is perfectly okay. All the .php extension is doing is running that file, when called, through a program on the server that looks for code between "<?php" and "?>". If it doesn't find any, it simply passes it on. Granted, theres no point on naming a purely HTML file as a php file, but unless you're just serving up millions and millions of pageloads a day, you likely wouldn't notice the effect of sending a purely HTML file through the PHP parser.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.