Wickham's HTML & CSS tutorial

Sitemap | Home | Search | Form 1

Introduction to PHP

View in Firefox, Safari, Opera and IE but IE6 often needs different solutions. In general IE7 and IE8 display like Firefox apart from the HTML5 and CSS3 features. The IE9 & above updates are mainly related to HTML 5 and CSS3 issues and display to a large extent like other main browsers. Google Chrome is based on the same WebKit engine as Safari.

Some of the examples are provided just to show problems, others show solutions that work in most major browsers. Use the example that suits you.
red icon problems   gold icon warning: works in some situations or some browsers or needs care to display as you wish   green icon OK in Firefox, Safari, Opera, Google Chrome and IE

The main purpose of this tutorial as a whole is to show how IE and Firefox differ, although PHP should not affect the differences.

Some PHP basic information

PHP (PHP Hypertext Preprocessor) is used to make webpages dynamic; for instance to process forms, carry out online shopping, feed to and from databases, etc. but there are many other applications, many of which overlap what other languages like JavaScript can do, or just carry out advanced functions on a static page.

PHP is a server-side language, which means that the code will not be processed in a browser as it's processed by a viewer's hosting service or a program like Apache on a computer. When you code PHP you will have to use a server on your computer or upload it to your host to see the result. Javascript is a client-side language which is processed by a browser.

You can download PHP 5, Apache server and MySQL database individually but most people download software that has all of them bundled together plus phpmyadmin for use with MySQL. Windows computers should use WampServer 2 while Linux users should use XAMPP (there is also a Windows version of XAMPP).

WampServer for Windows: installation and startup

I have WampServer and have prepared this tutorial using it. You can download it from this site.

PHP versions

Some hosting services may not have updated their PHP version from 4 to 5. If you are coding PHP for your own hosting service you will know whether you can use new features in PHP 5 but if you are coding for clients you should check whether their hosting service supports PHP 5 and even if it does, be aware that they may change hosts to a service that does not support it.

PHP 5 introduces new features, the most important of which is Object Oriented PHP (OO PHP). This allows classes to be set up and separate chunks of code can be held in separate files and referenced by classes which saves code on an individual webpage and can save time when coding lots of pages as you can fetch pre-prepared sections of code from separate files.

Some basic codes used in the form examples listed later

Make sure you see the difference between curved brackets ( ) and curly brackets { } as it isn't very clear, but using zoom if you have it will make them more clear.


The code:-

The output is:-

TV Emporium

Name: TV Emporium
23 High Road, Newtown 45678

Name: TV Emporium
23 High Road, Newtown 45678

Note that numbers in variable $zipcode = 45678; do not need double quotes.

The output can be by echo or print. The first print is just the variable and the next two have text and html tags. The long middle code has periods . between each text or html tag section in double quotes and each variable which is not in quotes; even spaces have to be inside double quotes. There is no need for spaces between each section like the spaces between "<p>Name: " and the period and $company which are just there for clarity. The insertion of periods is called concatenation.

The last method puts the whole code inside double quotes but this time you need to place a space where required like between Name: and $company and between $street_address and $zipcode.


The code for an indexed array:-

The output is:-

The second member is:- Paul S

The array strings are in double quotes and separated by a , comma. Note that for print or echo, arrays start with 0 then 1,2, etc; so the second part of the array is [1]. The echo statement could have the number in single quotes ['1'] but generally numbers do not have to have quotes.

The code for an associative array:-

The output is:-

Our cheapest bed is:- £200.00

Our cheapest bed is:- £200.00

Note that ['Bed'] is inside single quotes in the print line and the last print uses concatenation to put everything inside one print statement.

Conditional statements

Basic codes for a conditional statement:-

The output is:-

No it isn't

It asks a question and if the answer is true it prints or echos the statement, if untrue it moves on to the else statement (there can be ifelse conditional statements in the middle). In this case 1>3 means 1 more than 3 so the answer is untrue. There are { } curly brackets around the echo statement and there is no semicolon between if(1>3) and { echo nor between } and else

The output is:-

No, names are different

Note the double == sign. One equal sign = gets the value, for instance a variable getting a value from a text string; two == checks if it is equal to a value and three === checks if it is equivalent or exactly the same. These subtle differences are the subject of a more advanced tutorial.


A "while" loop:-

The output is for a "while" loop:-

Hello 1
Hello 2
Hello 3

The ++$count repeats the echo while $count is less than or equal to 3 and you need to include a space if required inside the echo " " text string. There are { } curly brackets around the echo statement and there is no semicolon between while ($count <= 3) and { echo

A "for" loop:-

The output is for a "for" loop:-

Loop 1
Loop 2
Loop 3

Note that the variable $count = 1 is now inside the ( ) which extend to include ++$count and there is no semicolon between ++$count) and { echo

A "foreach" loop:-

The output is for a "foreach" loop:-

Item: bread
Item: meat
Item: fish


str_word_count string word count

The output is for the function str_word_count is:-


The above codes show two ways to achieve the same result of counting the words, with a <br> tag in between. echo str_word_count("This is just a test sentence"); uses the function directly while echo $wordnumber; uses the variable created in the first line.


The output is for the function mail is:-

Email sent: 0

The mail function on its own opens the client email program and fills in the data. This function coupled with the variable $mailsent and the echo returns a number 1 if true that the email was sent or 0 if false and the email was not sent so that the true or false numbers can be just seen or used in further processing. However, I have disabled the function and shown a typical result as I don't want real emails to be sent. Emails in PHP need a lot of advanced coding to weed out spambots and errors.

simon@xyz.com is inserted as the email addressee, Website update is for the email subject and Message about updating is for the email body.

Alternatively the mail function can be combined with a conditional statement and a text result:-

The output is for the function mail is:-

Email failed

strpos strip position

It has the @ character

The strpos function is useful in checking whether an email address is valid, but other checks on email addresses are also important. The character you are checking goes in the last set of double quotes. However, you cannot use the @ character in the function like $char_@

strip_tags strip the tags and stripslashes strip the slashes

Tags: This is just a test sentence with tags which should be stripped.

strip_tags is useful if you have a text string taken from web page code and you want to display the text without the tags.

stripslashes is another useful function because entries by a viewer into a web page form like Fred's name will create a slash before the ' when you receive the email or database entry, so using stipslashes before the form input variable like $name = stripslashes($name); will remove the slashes in the email and also in any echo confirmation text or "Thank You" page.


Here are two sessions tutorials:

Note that the following tutorial may not work and your page will show an error:
Warning: session_start() [function.session-start]: SAFE MODE Restriction in effect. The script whose uid is 777 is not allowed to access /tmp owned by uid 0 in /home/your-username/public_html/your-directory/your-filename.php on line 1
Fatal error: session_start() [function.session-start]: Failed to initialize storage module: files (path: ) in /home/your-username/public_html/your-directory/your-filename.php on line 1
You need to disable safe mode to access the tmp folder.

Put this at the very top of your main PHP page above the doctype with no space in front or above it:-

Put this in the main PHP page body section which will show the date and time the page was opened:-

which will display the date and time of the session.

It is supposed to be possible to use date(DATE_RFC822) or date(DATE_RFC2822) in the code above the doctype instead of date('r') but I find that it shows date and time correctly in WampServer but corrupted when online and I'm not sure of the reason yet so I've used date('r') for this page.

You can show the date and time separately with this above the doctype:-

and this code in the body:-

You can find some useful codes for date and time on the php.net site.

You can see the record of the date and time the page was opened by recording it in another PHP page by using this code in the first main PHP page for the link (and keeping the code above the doctype):-

and this above the doctype of a new PHP page called session.php:-

and this code in the body of the new PHP page called session.php:-

The output on the second page session.php will not change when using the refresh button, only if you go back to the page where the session was set (phpintroduction.php in this case) and refresh that page.

Sessions remain open for 24 minutes on the second page session.php which might form part of an online transaction. This is usually long enough for someone filling in an online purchase form although more complicated code can extend this. If a viewer refreshes the original page the session will be restarted.

Other features that can be shown include the user agent (browser) by using this code in the main PHP page above the doctype and the link to session.php described above:-

and this code on the session.php page above the doctype as before:-

and this code in the body section of session.php:-

You can print out the content of your session code for checking using print_r( ); it's not exactly validation which is done by the PHP engine showing warning notices if the code is faulty, but this is a way of checking what you have included in your session code if it gets complicated.

Sessions are recorded in a cookie by the PHP engine which uses a unique id. You can see this id (you don't normally need to see it) by using:-

Examples of PHP coding

1 green icon A simple form with basic code explanation.

2 green icon Forms to send data to an email address.

3 green icon Form processing to a database and printing output on a web page.

4 green icon Object Oriented PHP using separate class files.

5 green icon Server-side "includes".

6 green icon Show a form on the same page after clicking a link.

7 green icon Show a News Box on the same page and show different news items in the same box after clicking one of several links.

8 green icon Date, Time and future dates.

9 green icon HTML Emails.

PHP tutorials and useful sites


View/Source or View/Page Source in browser menu to see all html code.

The body of this page has margin: 20px. The examples above are in a containing div with width: 730px; and margin: auto; so that the page centralises at large screen resolutions.

A lot of codes have been put in html tags in my examples rather than in a stylesheet or in a style in the head. I have done this for the convenience of the viewer so that most (but not all) codes are in one place and the stylesheet does not always have to be viewed in addition. When coding your own page you should try to put as much as possible in a stylesheet and link with id or class to the html tag.

Remember that when a Doctype is included above the head before the html tag (as it should be) then the overall width of a div is its defined width plus borders, margins and padding widths.

If there are differences between Firefox and IE6 that cannot be overcome with one code, code first to get a satisfactory solution in Firefox then create an IF style which will only apply to IE6:-
for instance, if margin-top: 20px; in Firefox needs to be margin-top: 30px; in IE6 then put the following in the head of the html/xhtml page:-
<!--[if ie 6]> <style type="text/css"> div { margin-top: 30px; } </style> <![endif]-->
or if there are many different styles, create a separate stylesheet:-
<!--[if ie 6]> <link rel="stylesheet" href="ie6.css" type="text/css"> <![endif]-->
IE6 will contain just the amended styles such as div { margin-top: 30px; } and others (no head or body tags or Doctype).

When looking at a page source for this site you may see code like &lt;p>Little Egret&lt;/p> instead of <p>Little Egret</p>. The code &lt; is because in that instance the code is to be displayed on the screen as text. If the < symbol was placed in the code a browser would activate the code and it would not display as text. Such code is not normally required when writing html code tags which are to be activated.

© Wickham 2009 updated 2013

top | Sitemap | prev | next


web www.wickham43.net/