user6813459
user6813459

Reputation:

Is it safe to leave uncommented PHP code in HTML files for later use?

I'm working on a website where I use PHP for some stuff. The PHP is finished and I'm moving to designing the page now. Since I'm working locally, I have to start up the server every time to preview the page. That's kinda inconvenient because lot of the times I get some idea when I'm not working on the site and server is not running. So my question is:

Is there's any harm in leaving the PHP inside and just change the file extensions to HTML?


I just tried it expecting the PHP code displayed on the page but surprisingly, all the PHP code got commented out. I remember that when I was using Chrome on Windows couple years back, it didn't worked like that (now I'm using Safari on macOS).

Do PHP tags in HTML file get automatically commented out nowadays, or is that a browser thing?


I'll use the PHP in the finished site anyways, it's just so that I wouldn't have to start server each time.

BUT I'm interested how this works, because It might be useful in the future. For instance if I had some pure HTML/CSS/JS website that would sometime later use PHP, so I could code the PHP upfront and just have it commented there for later.


EDIT: Here are screens from Safari. The source code is uncommented, but in the Inspector window it is.

Source: Figure 1 - Source Code

Inspector: Figure 2 - Inspector

Upvotes: 0

Views: 97

Answers (1)

Brad
Brad

Reputation: 163301

Since I'm working locally, I have to start up the server every time to preview the page.

This should be a non-issue. You should be using a local development server if you're trying to test things in-context.

Is there's any harm in leaving the PHP inside and just change the file extensions to HTML?

Well, yes, your PHP code is going to be all over the place, potentially having unintended side effects, breaking your page validation, etc.

Do PHP tags in HTML file get automatically commented out nowadays

No. Your PHP code is definitely there, messing up your page in ways you might not see yet until you're busy trying to debug something else and find that your random text in your HTML page (your PHP code) is breaking it.


Update, since your screenshots: Nothing is actually commented out in your page unless you commented it out. Safari is throwing you a bone by showing in the DOM that it treated your invalid markup as a comment. It's being nice to you.. don't rely on this.

Upvotes: 3

Related Questions