Continued from Part Two .
This is the third in a planned six part tutorial on PHP and building web sites.
What you will need for this tutorial :
- You will need either PHP enabled web space provided by your ISP, or a PHP enabled web server installed on your own computer.
- You can create PHP pages the same way as you do HTML pages, so no specific editor is required or assumed, use what you normally would in your web site creations.
- Finally, this tutorial assumes you are familar with the basics of web design in general and that you understand the basics of HTML coding. Future Tutorials may require the presence also of the mySQL database, but we only touch on it with a simple example. This six part series is not set in stone as to which route we are taking, so please, if you want something specific covered, let us know.
Step 1 - An example in time
step 1 - An example in time, using the date() function
This example shows a number of things. It shows obviously that we can use PHP to get the current day of the week, based on this, we can create different scenarios and base our output depending on the outcome - using statements in our code to the effect of IF it is Monday then do something, IF it is Tuesday then do something else, and so on. Using IF or Switch statements we can pretty much decide what happens next, or what gets displayed on the web page - this is not restricted to time or date but can be anything :-
The (verbose) code to produce the above line is :-
<p> <? $today=gmdate(l); switch($today){ case 'Monday' : $color = 'red'; break; case 'Tuesday' : $color = 'orange'; break; case 'Wednesday' : $color = 'yellow'; break; case 'Thursday' : $color = 'green'; break; case 'Friday' : $color = 'blue'; break; case 'Saturday' : $color = 'indigo'; break; case 'Sunday' : $color = 'violet'; break; } print '<div style="color:'.$color.'">Today is '.$today.', so we like the color '.$color.'</div>'; ?> </p>
Looking at this code, you can indeed check the output and see that it matches correctly with the day of the week. I mentioned use of the date() function, but have in fact used the gmdate() function, which is the same except that it bases itself on Greenwich Mean Time (GMT).
step 2 - The < title > problem.
We mentioned in the last tutorial that in the header.inc file we created, that there is a problem in the fact that the title element is within this file, and if included in every page of our site then the title will be the same in every page of our site. This is obviously an undesirable effect and one we will cure now by introducing a variable.
As a reminder, our header.inc file looks like this :-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" > <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> </head> <body>
As you can see,
<title>Untitled Document</title>
is what we call static content, it never changes, so we need to alter this, change it into a variable so that it does change based on circumstances - that being of the page that includes the header at the time. So first we create a variable in our header.inc in place of our static title content, then we use the pages that call the header.inc file to tell it what the variables information is. We also create a generic title just in case we forget or decide not to create a title variable in our main pages.Open up your header.inc file, remove
<title>Untitled Document</title>
from the code and replace with :-<title> <? if (defined ('TITLE')){ print TITLE; } else { print 'Welcome to My Site'; } ?> </title>
So, what we are saying in our code above is, if the 'TITLE' has been defined - created
, then use it, if not then we use a generic phrase 'Welcome to My Site'.
Save your newly altered 'header.inc'. Now we need to take advantage of this new code in our
main site files that use (call) header.inc - which is just about all of them.
Open up 'simple2.php' from Part One tutorial, a reminder of the content :-
<? include 'header.inc'; ?> <p> This is a default site template page.</p> <div id="navmenu" > <? include 'nav.inc'; ?> </div> <? include 'footer.inc'; ?>
Right at the top of this code is where we call 'header.inc', so just before this, we need to create that 'TITLE' content so it can be passed in to 'header.inc'. The code for this is simply :-
define ('TITLE','This is my simple page');
Our modified main content page now looks like :-
<? define ('TITLE','This is my simple page'); include 'header.inc'; ?> <p> This is a default site template page.</p> <div id="navmenu" > <? include 'nav.inc'; ?> </div> <? include 'footer.inc'; ?>
Save this as 'simple3.php'. And our modified 'header.inc' now looks like:-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" > <head> <title> <? if (defined ('TITLE')){ print TITLE; } else { print 'Welcome to My Site'; } ?> </title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> </head> <body>
And they should work beautifully together .
Summary
This Part Three tutorial was two-fold, I wanted to introduce some more features of PHP to show newcomers to the language what can be acheived, and the sorts of things that web developers use it for.
The second part was to re-visit the problem raised last tutorial regarding the 'Title' element. The cure for this was to introduce a 'constant' variable (now theres a contradiction). We have a file that gets used by many other files (pages) but they each need to add their own individuality to the included content. The 'constant' was ideal in this situation - a value that is different depending on the calling page, but once set will never change. This is the main difference between the 'constant' and a true 'variable' which value may be set initially but could change throughout its lifetime.
Related Tutorials
Introduction to PHP - Part One
Introduction to PHP - Part Two
Introduction to PHP - Part Four


