Tealmermaid Designs

web design, coding, and SEO tips and tricks

Codes for PHP


php codes
PHP (Hypertext Preprocessor) is a programming language used to manage online content via scripts similar to CGI.

PHP is focused on server-side scripting PHP scripts can be used on any server which has PHP enabled (check with your web host). It can be used like any other CGI program can do such as collect form data, generate dynamic page content, send and receive cookies, access databases, or output text and images. This makes PHP a useful tool for web development.

Create your first PHP script

We will begin by creating a "Hello World!" webpage for your first PHP script.

First create a PHP file called test.php.

Note: make sure the *.php inside of the filename is used as extension for the file rather than part of name (not test.php.txt).

Open your file in a simple text editor such as TextEdit and type the following:

echo "Hello World!";

Save the file, then upload it to your web server. Access the file via your web browser. It should display as follows:

Hello World!

As you can see, once the file is on the server, the web browser can execute the PHP script. PHP code is denoted within the script with the tags <?php (open tag) and ?> (close tag). echo is a PHP function which displays whatever text is placed within the quotation marks. Each line of a PHP script must end in a semicolon, else it will throw out a parse error.

This is extremely useful for automatically updating the copyright date in your footer:

echo date("Y");


This can be used with any of these variables to create a 'last updated' line for a page:

Note: If your server is in another time zone, you may have to adjust the time function:

echo "The time is " . date("h:i:sa") . " in Chicago";

This outputs the following:

The time is 04:12:47am in Chicago

PHP includes

Probably the single most useful feature of PHP is the ability to create includes. For a standard web site created in HTML, every piece of code on every page must exist. In PHP however, we can place repetitive sections of code into another file and "include" it on any page where it should appear. This is especially useful for common site features such as headers, footers, menus, and advertising since if you need to make any changes, you will only need to update the include file in which the code appears rather than update every single page of the site on which the code appears.

How to use PHP includes

Open a page of your site such as the index page. Save the file as a .php file rather than an .html file (index.php for example).

Once that is done, cut the code for your site's header (HTML header tags, doctype, meta tags, stylesheet link, and site logo) and paste it into a separate file called header.php. Do the same with your site's footer (into a footer.php file), and so forth for any piece of code which appears on multiple pages. The files for your site (index.php, about.php, etc) should only include code which is unique to that page.

Now go through all the other pages within your website, rename them to a .php extension. Delete the header, footer, etc code from each page as it is removed.

You can add PHP includes to the top and bottom of each web page such that the includes call the code from the header and footer files. To do this, open index.php in your text editor. At the very top of the file, add the following line of code:

<?php include("header.php"); ?>

Repeat at the bottom of the page for the footer to include:

<?php include("footer.php"); ?>

If you have any other includes, add that include code at this time following the same form. Then copy it in the relevant places on the other pages of your site. Save and close each file as you finish with that file.

Once complete, upload all the files to your server. Test all the pages to make such they are still working correctly. If you view the source code of the page, the code will look the same as it did before you converted the pages to PHP because the PHP code runs on the server such that pure HTML is seen by the browser. This means that no one will know that you have used PHP includes because the component files (header.php, index.php, etc) are joined together before visitors sees them.