minitutorials main logo the place to learn online

Sponsor This Site!
Sponsorship Details - miniTutorials is taking enquiries into various Sponsorship opportunities. Available NOW! Read More on our Sponsorship page.
This Page Sponsored by :
Proudly Sponsored by Your Company name here!
miniTutorials Sponsorship
Donate!
Consider donating to keep this site alive and up to date. Click on the Donate button to go straight to PayPal site and make a donation of an amount you choose.


More information on where the money goes on the Donations page.
Discussion Forums
visit the mini tutorial forums
For expert help and advice on any of our Tutorials or anything else .... visit the forums and ask away!!
Google Ads
My Profile

Details about the owner of this site can be found at :-

ohloh profile
View Gavin McDonald's profile on LinkedIn
Powered by a
UK2 Dedicated Server

PHP Tutorial

Introduction to PHP - Part Four

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 :

 

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. - Saturday) 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>

Top

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

Top

tutorial sponsors logo

About Us | Site Map | Privacy Policy | Contact Us | ©2002 - 2007 miniTutorials.com