Some Prompt Here
Cross
PHP - Day 33 - May 13 Posted about 1 month ago
digg
delicious
stumble
reddit

Slowly getting back to normal - so getting back to my PHP studies was important - even if I couldn't actually finish an entire example.

Today's progress:

Using “elseif”

This is similar (uh oh) to the “if-else” conditional we have already studied. Now to see if I fare better with this than with the nested conditionals.

An example: If (condition1) {
statement(s);
} elseif (condition2 {
other statement(s);
}

You can add an “else” conditional at the end of an “if-elseif” conditional, but you have to remember it should be the last thing. This is the ‘default’ behavior, if none of the condions in the code are TRUE. There is, however no limit to the number of “elseif” conditionals you can use.

Hoo boy - the example is a long and complicated nested conditional to find the registering user’s astrological sign. I failed totally with this before, but maybe doing this one will give me the insight to see what I was doing wrong with the other.

This uses the existing “handleReg.php” page I have already made, but just adds to it.

After all the existing code, but before the closing PHP tag, we build our new conditional.

Here goes: if ($month == 1) { //This is January

        if ($day <= 19) {

$sign = "Capricorn";
} else {

$sign = "Aquarius";
}
}


print "Your astrological sign is $sign";

This basically says - if you chose January for your birth month, and the day was the 19th or before, then your sign is Capricorn. Otherwise, your sign is Aquarius.

Testing this baby out piece by piece - so added that “print” command and uploaded it. Tested it, and it worked!

Now to add more - the code for each of the month-day combinations is essentially the same, only differing in the day range and the sign - so I’m not
putting them in here - too repetitious and long - but I will test each bit as I go.

February added - tested - works!
March added - tested - didn’t work - I had spelled elseif as elesif - presto - blank
page. Corrected that - tested again - works!
April added - tested - works!
May added - tested - works!
June added - tested - works!

And I know I should do more….but I’ve run out of steam for today….and this has taken way more than my daily 15!

You can test out the astrological sign thingy as long as you stay in the months already coded - I found it really interesting that I could just make up the $sign variable on the fly as it were.

Check out the progress here:

www.shimmershine.net/register.php


Recent Comments

No comments yet.

Please login to comment.

Back