The 'If' Statement - Part 1
by Varun ThirayanAPDZ.com
Thursday, 23rd February 2006
First of all we need to look at what goes into making the statement itself.
Below is only a method of understanding, it isn't actually PHP and won't work in a browser.
if (Something is 'true') {
Do everything inside these brackets
}
The next question probably going through your head is, 'How is something true?'. Well there is a selection of 'operators' which create this part of the statement. Below is a list of the most popular operators used. It is by no means complete and I'm sure that is more out there!
!= (or) <> Is not equal to
> Is greater than
< Is less than
>= Is greater than or equal to
<= Is less than or equal to
Using the above ideas we are now able to create a wide variety of mini-scripts to decide what to do next... One idea is a simple 'age-checker' which could be developed later on for use in a registration form or something along those lines.
Age Verification
We are going to make an all in one script which will:
1) Ask the user what their age is
2) If the person is older than 14 - do something
3) If the person is under 14 - do something else
4) It sounds simple... and well... it is!!
The file is going to be called 'age.php' and should be written in a plain text editor - Windows Notepad is perfect for the job. I'll type it up first and then explain it all afterwards.
if ($_POST) {
$age = $_POST['age'];
if ($age > "14") {
echo "You are older than 14";
} else {
echo "You are younger 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 question buzzing around your head now is 'How the does it all work!?!?!'. I'll try my best to explain...
The Big Explaination
I'll run through each bit individually and you should get the drift...
if ($_POST) {
If the '$_POST' variable has been set - ie. someone has allready sent their age to the server, then php will go through the code in this section of the script... If however, the '$_POST' variable has not been set then...
} else {
this part of the code will be used - ie. display the form, so that someone can send their information to the server...
}
?>
One little thing to note... the '$_POST' variable is an array (not important right now) which is automatically set to be 'true' when variables are sent (from a 'post' form) to the server. That's why it's used here. It's an easy and effective way to see if someone has used the form.
It comes from this line: '<form action=age.php method=post>'. As you can see we are sending the information using the post method, there are others available but this is one of the easiest to use in this case.
Enough talk, more php...
if ($_POST) {
} 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>";
}
?>
Above is basic html - hopefully you allready have a good idea of what it all means... but what the heck, I'll run thro it anyway... (Check out the working example to see the appearance - which hasn't been changed - just for ease - of course you can if you really want)
It is placed in the part of php which is used when information has not been passed to the server (the 'else' bit) - the user has not sent any information so needs a form to do so... lets not go on about that tho...
The first line of html - a title... ok... yes... not much more to say about that...
The next line is slightly more important. The first tag, starts the form section and uses the post method as we have allready stated. It also uses the name of the file that we are working in - there is no need to send the information to a seperate file. The next tag - a basic textual input. The important bit though is its name - 'age'. Keep that in the back of your mind, we'll be using it soon.
And finially the last line is the all important submit button which actually sends the information and the end form tag... done.
Have a look at the source (in IE - click 'view'->'source') in the working example - notice that no PHP whatsoever is printed, only the part within the echo statement - ie. the HTML.
if ($_POST) {
$age = $_POST['age'];
if ($age > "14") {
echo "You are older than 14";
} else {
echo "You are younger than 14";
}
} else {
// Show the form as allready stated above
}
?>
We are now seeing the true power of our script!!
We have allready decided that this part will only be called upon if a user has sent information to it... right?? So the user has typed an age, and then clicked the submit button.
As I said briefly earlier, '$_POST' was an array. There is no need now to go into the details, but one part of that array is '$_POST['age']' which contains the value that the user typed. On the first line after the first 'if' statement, we have set the posted variable to a 'local' variable. This means that '$age' now has the same value as '$_POST['age']'. There is no real reason for this apart from me not wanting to type $_POST['age'] everytime I want to use its value - so I gave it a shorter variable name to make it easier.
In another one of our 'if' statements - we check to see wheather '$age' (the variable we just set) is greater than '14'. If it as - as im sure you know know - PHP will carry out the information within the braces, that is 'echo' out 'You are older than 14'.
Then comes the else part of the 'if' statement, that is what happens when '$age' is less than '14'. In this case, 'echo "You are younger than 14";'.
And that, ladies and gentlemen, is it. This is pretty much as basic as basic gets. Within the braces, we are echoing one or two lines - in reality, thousands of lines of code can be used within one set of braces, it may not be practical, but it is possible. This code also isn't what you would call secure. The user could type anything atall - their name for example and the php would get confused and probably think that this is greater than 14. Try it yourself to see. It would need some work on it in order to actually make the outcome realistic. And anyway, even if the script only accepted numbers - there is no way of testing wheather the person is telling the truth. So maybe not a practical example - but it should help explain it for you!
Something to do when you're bored
There are loads of little mini-scripts you can write using the if statement - heres two ideas to keep you going...
Password Idenification - Very similar to the above script but maybe using the '==' operator to check to see wheather the inputted password is equal to a certain one. (Obiviously this isn't a secure method so don't rely on it too heavily)
Checking age using a date of birth - A slightly more complicated one using a couple more 'if' statements and maybe some knowledge or HTML drop-down boxes to check the persons age. Would involve a bit more work... if you can be bothered. (notice I used to word 'if' there)
If you do decide to make something using the 'if' statement then, if you can, upload it to somewhere on the internet and post the link in a comment below.
Before the comments roll in though, I am well aware that the 'if' statement goes further than what i've written about - there is much more to it but lets just keep it simple!
Options:
Printer Friendly
