The use of the ".php" extension

General question…

My home page and sub-pages end with .html. I’m told .php is better in case I want to use php sometime in the future.

Yet, when I experimented with one page and changed the extension from .html to .php, the result was just a txt doc. Before I make such a change to all my pages, my question is: will my server still present it as a web page when the address is changed from:

mypage.com/subpage.html

to:

mypage.com/subpage.php

Also, do I change the index page (home page) from .html to .php as well? i.e.

from:

index.html

to:

index.php

Thanks.

You can run PHP on .html pages by adding a little setting to your server, but yes, it’s better to have a .php extension on all of your pages (including the main index page) if you want to use any PHP.

The thing you first need to check is that PHP is actually installed on your server. If it isn’t, your hosting account is cough (crap) cough, but it is easy to check.

Simply paste this code into a .php file (e.g. test.php) and upload it to your root folder.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PHP Check</title>
</head>
<body>

<? phpinfo(); ?>

</body>
</html>

Then navigate to yoursite.com/test.php. If you get a long page of PHP Info, you are good to go. If not, you are <snip></snip>!

The fact that you got a .txt file when you tried to use a .php file has me very worried. Anyway, try out this test and let us know how it goes.

Once you’re done testing, delete the file from the server again, as it’s a security risk.

To ralph.m,

Tried it and yes, it works and I get a veeery long page of php info… a purple and gray chart with: “PHP Version 5.2.17” at the top of the chart.

Re: I get a txt file when I save it as “test.php”… for some reason .txt is also added when I saw it listed locally (i.e. test.php.txt… could be because I tested it in Notepad… gotta stop using it), so I just renamed it and removed the .txt portion, then Refreshed, then sent to my server, and then tested it… it worked okay.

Then I removed the test.php file as you directed.

Thanks for your input.

That’s a useful little goodie, Ralph, concise and detailed. We should consider adding it to the forum sticky.

The page to use to test should be just

<?php phpinfo(); ?>

as that includes all the necessary tags and any you add around it are just duplicated.

PHP info, is probably the first file you ever make when learning PHP. I wouldn’t totally agree with keeping the extensions as *.php being better but I suppose it would be simpler for a beginner.

Yes, your problem is you are saving with *.txt as the extension so either enable the viewing of extensions by unchecking ‘Hide extensions of know file types’ or be more careful when saving in notepad.

Thanks all…

Here’s a question a bit off-topic, but it still pertains to my coding…

My browser doesn’t recognize apostrophes, colons, quote marks, etc. It results in empty white squares where the punctuation marks should be… see my screen dump:

http://www.blog-guy.info/box.png

Aside from using special code for a double quote and a colon, etc, what else can I do?

Or… is it a result of me still using IE.

Thanks!

Use Unicode, use UTF-8 and save as UTF-8. Have you been copy-and pasting text by any chance from a word-processor?

Thanks Mike. Yes, it might be handy, though there’s more that could be said, like how to use PHP without a .php extension. And it would be better to clean out the jokey bits.

Anyhow, I’m probably not the best person to do this. It only took felgall a few seconds to point out that I don’t know what I’m doing!

Thanks for that Stephen. Had no idea about that. I’m sure the book I got it from had the page code, but maybe I’m wrong.

As xhtmlcoder said, be careful where your code is coming from. Ideally, use a proper code editor. There are plenty of free ones, like Notepad++ for Windows and TextWrangler for Mac.

Your page is already set to use utf-8 encoding, so hopefully you will be ok on that front. (Unless your server is set to do something different, which can override this.)

Not from a word-processor, but from one Notepad doc to another while updating my page.

And I just checked my page in both IE and Firefox.
In IE there are little empty squares.
In Firefox there are small squares with letters inside… something like:


|FF|

FD

So, since my code is already set to use utf-8, what is the “something different” that my server might be doing… and how do I check for that?

Thanks.

I think you can just go to View > Character Encoding (in Firefox) and check. I’m pretty sure that give the server encoding, but not 100%. Anyway, it’s UTF-8 as well, so no problem there.

Addendum…

Since I’m converting all my html files to php, I have a concern about how the DocType is listed at the top of each page of code. Presently it says:

“<!DOCTYPE html PUBLIC…”

Should I change that to read:

“<!DOCTYPE php PUBLIC…”

Thanks.

No!

On your home page, you have this at the top of your page, which you should use on each page, except with different, page-appropraite tile, keyword etc:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html
	xmlns="http://www.w3.org/1999/xhtml"
	lang="en"
	xml:lang="en"
><head>

<meta
	http-equiv="Content-Type"
	content="text/html; charset=utf-8"
/>

<meta
	http-equiv="Content-Language"
	content="en"
/>

<!--
	keywords should be 8 or 9 words relevant to the page,
	not TWENTY-THIRTY. Also, try to keep it under 80 bytes
	in length!
-->

<meta
	name="keywords"
	content="blogs, links, index, search, blog-guy"
/>

<!--
	description should be the text you want to show on your SERP
-->

<meta
	name="description"
	content="Links to blogs covering a wide range of topics"
/>

<meta
	name="verify-v1"
	content="gXJGEl+G1fa/WB+rIsYMCSavGfqia7kgllCVEczYlgI="
/>

<meta
	name="google-site-verification"
	content="umEpFAqj3Vpv6_A_2zF3TZxuw34t0Iu_yzPa2DJogK0"
/>

<!--
	CSS links should have a MEDIA type on them to say
	WHAT media you are styling, so you don't send style
	to devices that will screw it up like handhelds or
	print
-->

<link
	type="text/css"
	rel="stylesheet"
	href="screen.css"
	media="screen,projection,tv"
/>

<title>
	Blog-Guy.info
</title>

PHP processes your pages on the server, making any changes necessary. But after that’s done, the page is sent to the browser as it was before, and it is still a HTML file. The .php extension just tells the server to run the page through the PHP engine before sending it on.

To ralph.m,

Clear and concise… thanks.

In Firefox there are small squares with letters inside… something like:

That’s the symbol Firefox uses to mean “I don’t know what this character is”. Mismatched charsets between how a document was saved and how the server sends is a popular method of getting little boxes/question marks/FBFB boxes on a page.

The document has to be saved
the same way your PHP is wrangling it
the same way the server serves it
the same way you state it in the meta tag (this is the least important one though as the server will always override this tag if they conflict)

To xhtmlcoder, ralph.m and Stomme poes,

The wierd thing is the doc appears normally in my Text Editor. Whether I look at it in Notepad, Notepad++, or Crimson the doc in question doesn’t have those little squares. They appear only after I load the page to my server and look at it on the Internet. They appear in both IE and Firefox. As all my code in all my pages includes “charset=utf-8”, I’m perplexed by this (though when I save, the Encoding is set to ANSI… should I change that to UTF-8, too?). It’s not a big problem, just annoying seeing them on every page where I’ve used an apostrophe, colon, quote marks, etc.
What else can I do to get rid of them?
Thanks.

Addendum…

It pays to think outloud!

I went into one of the docs, resaved it, and changed Encoding to UTF-8 and the little squares are now gone and the punctuation appears normally. Now to make the change on ALL of them…

Still, why would it have done this when the charset was already set to utf-8?

Still, why would it have done this when the charset was already set to utf-8?

Because your meta tag will never mean anything if it doesn’t match the server…
but even if the server was also sending utf-8 but your document wasn’t saved as utf-8 then there is a mismatch… now the browser is trying to view an ANSI document but is told it’s utf-8…
kinda like hearing someone read Dutch words out loud but with English pronunciation… any Dutch person listening would not understand all of it and would be doing a lot of guessing which words were what.

It’s not that everything has to be utf-8, but that everything has to match. (and utf-8, despite what some people say, totally works with just about everything and supports way more languages and characters than many other charsets, so we recommend the one you choose is utf-8)

If it’s a brand new site then yes change the extension to php. But it should be noted that if it’s an established site by changing your extensions you will loose any google page rank you may have at least for a while. And any in coming links will be broken.

And any in coming links will be broken.

They shouldn’t be. That’s what mod_rewrite is for. Make’em 301 Redirects and all the googles update everything, without 404s.