Continued from Part Three .
This is the fourth in a planned six part tutorial on PHP and building web sites. This Part Four concentrates on reviewing and explaining code from Part Three. The next Part Five due out soon will introduce new code for you to play with, but I felt that this installment should make previous code clearer and at the same time set the scene for Part Five.
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 - Review of last months date() function and usage
Ok , so lets remind ourselves of that code again.
<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>
The previous Step explained the use of the 'if' statement, which is ideal when 2 or 3 possible outcomes are required. For more than 3 required outcomes then the 'if' statement can get a bit cumbersome to use, which is where the handy 'switch' statement comes in to play. Breaking it down a bit then
<p> <? $today=gmdate(l);
Opening HTML <p> tag followed by the now familiar opening PHP code section <?. We then assign a variable $today with the result of gmdate(l); - gmdate() is the date and time in GMT but by placing an optional string value inside the brackets, we restrict the output - in this case 'l' (lower case L) means use only the full Day (e.g. - Sunday) See http://php.net/manual/en/function.date.php for more options. The line ends with the usual ; .
Next comes the opening Switch statement which may be new to some
switch($today){
We have already assigned the variable $today with the result of the current day of the week, so we are using this result as an argument for the Switch statement. All options for a Switch are enclosed inside { curly braces. The format of a usual Switch statement is
switch($variable){ case 'answer1' : do something ; break; case 'answer2' : do something else; break; case 'answer3' : do something different break; case ... ... }
The break; just means that once a match has been found, then ignore all other statements in the switch block and move on. In our example therefore we are saying that if $today is Monday then $color is Red, otherwise if $today is Tuesday then $color is Orange etc etc...
Lastly we finish off with
print '<div style="color:'.$color.'">Today is '.$today.', so we like the color '.$color.'</div>';
Here we use the PHP print function to print out the result of the enclosed combined HTML and PHP code section. Rather than keep closing and opening PHP code blocks which can look messy when used in these circumstances, it is better to use either the print or echo function. This line of code needs more explanation and I will dedicate a section of the next Tutorial Part Five into doing just that, grasping what is going on here is fundamental to successful PHP + HTML coding for the web.
Finally, we close off our PHP section and close the remaining <p> tag.
?> </p>
Summary
This Tutorial Part Four was purely revision and explanation of code I introduced into Part Three. Usually I am an advocat of Do Now maybe explain later if asked. This tutorial series is aimed from Part One for beginners with the aim of getting you to an intermediate level of coding in PHP and decided I could not get away with carrying on regardless this time without further explanation. The Next installment will contain a little more explanation before going on to introduce some more concepts, code and examples. I have promised all throughout this series some integration of the mySQL database and will do just that in the next tutorial by starting a simple site login system.
Related Tutorials
Introduction to PHP - Part One
Introduction to PHP - Part Two
Introduction to PHP - Part Three

