![]() |
|
PHP Tutorial #3 Now you are familiar with the concept of loops, there are a couple more to become aquainted with. <?php
$i = 0;
while($i<5)
{
echo("i = $i<BR>");
$i++;
}
?>
The while loop is slightly different because some of the work that the for loop does has to be done by the programmer but is much more flexible. First we set a variable $i (i means index) then we start the loop which only waits for the value of $i to reach 4 as before (not 5 remember because we start from zero). The value of $i is printed and then incremented by one and then it goes around again as before and will give the same output as before. So why use it instaed of the for loop? Try to guess what would happen if the $i++ statement was before the echo statement. You would get this:- i = now: 1 Consider what is happening. The variable starts as zero but it is incremented before it's value is printed. This is just part of the flexibility of the while statement and we will return to it in a later tutorial. Now another loop:- <?php
$i = 0;
do
{
echo("i = $i<BR>");
$i++;
}
while($i<5);
?>
This is similar to the standard while loop but is started with the do statement and will give the same output as the first while loop. Try using them with different values of $i or perhaps use <P> instead of <BR> or perhaps even <HR><P> to give a line between each iteration of the loop. Alright, that's enough loops. On to something a little more interesting. <?php
$today1 = date("d M, Y");
$today2 = date("d/m/y");
$time1 = date("g.i a");
$time2=date("H:i:s");
$now=date("D F d Y H:i:s");
echo("$today1<BR>");
echo("$today2<BR>");
echo("$time1<BR>");
echo("$time2<BR>");
echo("$now<BR>");
?>
This uses the date() command of PHP to grab the date and time from the server and display it in the browser window. There are too many formats to list here but I have shown the most common ones above. This gives the following output:- 17 Apr, 2004 or at least it did when I ran it last. Now I'll show you how the computer can appear to be a bit more intelligent, able to make decisions based on variables and modify it's output. This is achieved using the IF statement. <?php
$hour = date("G");
$now=date("g:i a");
$msg="Good Evening.";
if($hour < 18)
{
$msg="Good Afternoon.";
}
if($hour < 12)
{
$msg="Good Morning.";
}
echo("$msg The time is now: $now<BR>");
?>
The if statement will choose between two statements based on certain criteria. First we obtain the current hour (between 1 and 24) and put it in $hour. Then the date and time is formatted and put into $now. We preset (initialize) the $msg which will ultimately contain part of the message we want to print. If the $hour is less < than 18 then it must be afternoon and the $msg is asigned accordingly. If it is less < than 12 then it must be morning. Various other tests could be added to include perhaps Good night. Finally, the two messages are displayed together to give a meaningful output. Note that variables have to be within the quotes unlike other programming languages one could mention. This is actually the kind of message that would be generated when logging into any networked computer system and so is a genuinely useful program (our first!). If can be a very useful command but it can also get a bit tedious where there are a great many alternatives to test for. This is where the switch case commands comes in handy. <?php
$num = 2;
switch($num)
{
case 1 : echo("Case 1"); break;
case 2 : echo("Case 2"); break;
case 3 : echo("Case 3"); break;
default : echo("Default");
}
?>
It is essentially a list of cases where we want to catch a particular variable value and react to it. So here we set the variable $num to equal the value 2. The switch statement then presents the variable and each is tested in turn for equality. Once found, the correct string is echoed to the browser and the switch is then terminated using the break command. Note that colons : seperate the case from the command to be carried out and is terminated with a semi-colon ; before the break statement. If the varibale is set to a number not contained in the case statements then the default command is used. Break is very useful and can be used with other loops: <?php
$i = 0;
while($i<5)
{
if($i == 3)
{
break;
}
echo("i = $i<BR>");
$i++;
}
?>
This will break out using the if statement to test for a value of 3 in the variable $i and the <5 statement is overriden. That completes this tutorial. More next time... Please eMail me at: nickjc@nickjc.co.uk if you need help |
|