You saw in the last section that variables are storage areas for your text
and numbers. But the reason you are storing this information is so that you
can do something with them. If you have stored a username in a variable, for
example, you'll then need to check if this is a valid username. To help you
do the checking, something called Conditional Logic comes in very handy indeed.
In this section, we'll take a look at just what Conditional Logic is. In the
next section, we'll do some practical work.
You use Conditional Logic in your daily life all the time:
In between the two round brackets, you type the condition you want to test. In the example above, we're testing to see whether the variable called $User_Name has a value of "authentic":
Conditional Logic
Conditional Logic is all about asking "What happens IF ... ". When you press a button labelled "Don't Press this Button - Under any circumstance!" you are using Conditional Logic. You are asking, "Well, what happens IF I do press the button?"You use Conditional Logic in your daily life all the time:
"If I turn the volume up on my stereo, will the neighbours
be pleased?"
"If spend all my money on a new pair of shoes, will it make me happy?"
"If I study this course, will it improve my web site?"
Conditional Logic uses the "IF" word a lot. For the most part, you
use Conditional Logic to test what is inside of a variable. You can then makes
decisions based on what is inside of the variable. As an example, think about
the username again. You might have a variable like this:"If spend all my money on a new pair of shoes, will it make me happy?"
"If I study this course, will it improve my web site?"
$User_Name = "My_Regular_Visitor";
The text "My_Regular_Visitor" will then be stored inside of
the variable called $User_Name. You would use some Conditional Logic
to test whether or not the variable $User_Name really does contain one
of your regular visitors. You want to ask:
"IF $User_Name is authentic, then let $User_Name have
access to the site."
In PHP, you use the "IF" word like this:
if ($User_Name == "authentic") {
//Code to let user access the site here;
}
Without any checking, the if statement looks like this:
if ( ) {
}
You can see it more clearly, here. To test a variable or condition, you start
with the word "if". You then have a pair of round brackets. You also
need some more brackets - curly ones. These are just to the right of the letter
"P" on your keyboard (Well, a UK keyboard, anyway). You need the left
curly bracket first { and then the right curly bracket } at the end of your
if statement. Get them the wrong way round, and PHP refuses to work. This will
get you an error:
if ($User_Name = = "authentic") }
//Code to Let user access the site here;
{
And so will this:
if ($User_Name == "authentic") {
//Code to Let user access the site here;
{
The first one has the curly brackets the wrong way round (should be left then
right), while the second one has two left curly brackets. In between the two round brackets, you type the condition you want to test. In the example above, we're testing to see whether the variable called $User_Name has a value of "authentic":
($User_Name = = "authentic")
Again, you'll get an error if you don't get your round brackets right! So the
syntax for the if statement is this:
if (Condition_or_Variable_to_test) {
//your code here;
}meet you in the next lesson.
0 comments:
Post a Comment