The 'If' Statement - Part 2
by Varun ThirayanThe previous tutorial, which created an age verification machine, had a couple of gaps in the code which, sometimes, meant that it didn't react in the way it should have. There were two main holes which, is this tutorial, we are going to tidy up.
- What happens if the text entered is not in a number form, ie. letters.
- What happens if the person types in 14
Here is the source code that we have so far:
<?php
if ($_POST) {
$age = $_POST['age'];
if ($age < "14") {
echo "You are younger than 14";
} else {
echo "You are older than 14";
}
} else {
echo "<h4>Age Checker Software!</h4>";
echo "<form action=age.php method=post>Your Age:<input type=text name=age><br /><br />";
echo "<input type=submit value=Go!></form>";
}
?>
The explaination of that one can be found in part 1 of the 'If' Statement tutorials.
Moving on now and it's time to attack part two. First of all, im going to make a couple of changes and give you the source then explain afterwards as usual.
<?php
if ($_POST) {
$age = $_POST['age'];
if ($age == "14") {
echo "You are exactly 14 years old!!";
} elseif ($age < "14") {
echo "You are younger than 14";
} else {
echo "You are older than 14";
}
echo "<br /><br />You are " . $age . " years old.";
} else {
echo "<h4>Age Checker Software!</h4>";
echo "<form action=age_part2.php method=post>Your Age:<input type=text name=age><br /><br />";
echo "<input type=submit value=Go!></form>";
}
?>
First of all - we have changed the name of this file. To keep this new version seperate from the original, the file name has been changed to age_part2.php to show that this is... erm... part 2. (You also have to remember to change the name of the actual file when saving!) So that when the user clicks the button - the information will be sent to this file. Thats a small change - but is pretty damn important.
Next up is the actual change to the PHP. Within the actual $_POST 'if' statement (which I droaned on about for ages in the previous tut) the content has changed to include an 'elseif' statement. These are called upon if the first part of the statement is not true. In this case if the variable $age is not equal to 14.
As many of these 'elseif' statements can be used as is needed, in our script though, one is more than enough. As you can see the elseif statement takes something within the rounded brackets () and outputs something else in the braces {}, just like our best friend, the 'if' statement. Finially to finish off this part - there is the else statement which has no rounded braces - it is just called upon if nothing else can happen. So in our code if $age is not equal to 14 and is not less than 14.
I added a little 'one-liner' after the 'if' statements to illustrate that this line will be called upon no matter what happens within the 'if' statement. No matter what the age is, it will allways show this line (which includes a little html, just to improve presentation). As seen in the variable tut, this will echo the age that the user entered (the variable $age)... just incase they forgot...
That now solves the problem of what happens when the user is exactly 14 years old but there comes a problem if the person doesn't enter anything atall or types in letters. So we are going to use another 'if' statement with a 'function' to clear this up. The bits that say 'new code' next to them are the new bits
<?php
if ($_POST) {
$age = $_POST['age'];
if (is_numeric($age)) { // New code here!
if ($age == "14") {
echo "You are exactly 14 years old!!";
} elseif ($age < "14") {
echo "You are younger than 14";
} else {
echo "You are older than 14";
}
echo "<br /><br />You are " . $age . " years old.";
} else { //
echo "You did not enter a number!!"; // New code here!!
} //
} else {
// Show html form - not showing it to save space Razz
}
?>
This nice new 'if' statement checks to see if the number entered is actually a number. If it is, then it runs the code that we have allready written, if it isn't a number (else) - then it echoes the statement which has been given, telling the user what the problem is... or whatever. Oh and this is obiviously carried out within the $_POST 'if' statement part.
This numerical check is done using function 'is_numeric()' which takes an argument in the brackets. This argument, or input, which is a variable name ($age in this case) is then checked to see if it is a number. If it is, then the function will return 'true', if the input is not a number, then it will return 'false'. As we know, if the information in the rounded braces is true - then the code within the braces is completed, ie. checks to see if person is 14 or older or younger...
Thats all for this mini-tut, it wasn't actually planned but was just written to fill in some minor details from the pervious tutorial. Anyway... all comments welcome as usual!!
Click here to see a working sample of the final part of this tutorial
© Varun Thirayan http://www.apdz.com