How to Use PHP on Your Web Page

PHP is a preprocessor which means that before your web page is loaded in someone’s browser, the server where the web page lives ‘pre-processes’ the web page. In other words, before the web page is delivered to the browser, the code for the page is first translated with the PHP translator on the server.

To use PHP, you must have a web server running. You can view your HTML pages in any browser with or without a web server running. For instance, if you create a file of text with a text editor and name the file with the .html extension, any browser should be able to open and display the contents of the file. The browser is smart enough to translate the HTML code (and CSS and Javascript) to display the web page.

Since Browsers don’t interpret PHP code, a web server must be running to pre-process the PHP code. The PHP code should create valid HTML, CSS or Javascript code. See my other post, How to Use PHP, Javascript (or jQuery), and HTML Together for more information about how all the languages work together.

Your hosting company must have the web server set up to run PHP. To make sure the PHP pre-processor translates your web page, you must give your web page an extension of .php instead of .html. This extension lets the web server know that the page needs to be translated or pre-processed before it is delivered to the browser.

Your PHP code needs to start with <?php and end with ?>. This lets the php translator know which code is to be interpreted or translated. That’s all you have to do to integrate PHP with your HTML code.

Let’s look at a simple example. I’ll show you the PHP code that shows you how your server has PHP setup:

<?php phpinfo() ?>

Put this one line of code in your web page. You don’t need any other code. Save the file as something.php – the name can be anything as long as the ‘.php’ is on the end. Put this file on your website – remember, it must be in a folder that is recognized by a web server as a website available to the public. You either have to have your local computer set up as a web server or you have to have a website somewhere. (If you don’t have one, contact me and I’ll give you a test website free for 30 days.)

Once the file is on your server, access the file through your browser – http://mywebsite.com/test.php – of course, use your website and your file name instead of the example that I have there. If your server has php installed, and if you got that line of code correct, and if you named it with .php on the end, you should see all the information about PHP on your webserver. Most of it will probably be greek to you, but it will let you know if PHP is running on your webserver. It will also show you what version of PHP your webserver uses and what extensions are included.

Don’t leave this file on your server. Once you know it’s working, delete this file. You don’t want anyone seeing this information except you. It could give them information that would make it easier to hack into your site.

I value your input. Let me know if you have questions, comments or constructive criticism!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top