Tuesday, January 11, 2011

Conditional Logic(if....else)

What is Conditional Logic?

Conditional Logic is all about asking  to urself or maybe with ur friends...."What happens IF ... ".For example, when u saw a  note "Don't Kick this Ball - Under any circumstance!",then u are using Conditional Logic. u are asking, "Well, what happens IF I do press the button?"

Another example of Conditional Logic in our daily life all the time:

"If I turn the volume up on my stereo, will the roomate be pleased?"
"If spend all my money on a new phone, will it make me satisfy?"
"If I take this course, will it increase my confident?"

ok let us,take this example:

$Name = "ali";

so,this variable is declare in "$Name" and it assign with "ali". We would use some daily Conditional Logic like this:

"IF $Name is ali, then let $Name have access to the site."

but in PHP, we use the "IF" word like this:

if ($Name == "ali")
{
//Code to let user access the site here;
}


So, this is the skeleton of if statement :

if ( )
{

}

So in between the two round brackets, u type the condition that u want to test. For example,

($Name == "ali")

Instead of using one if statements, we also can use an if ... else statement. Like this:

if ($Name == "abu") {
print ("His name is abu");
}
else {
print ("This is a wrong person...next!");
}


If we look at it closely, we’ll see that we have a If Statement first, followed by an “else” part after it. Here’s the “else” part:

else 
{
}

Ok,let me explain it how its works .So, we’re asking is: “Is it true that $Name holds a value abu ?” The variable $Name holds a value ali not abu, so PHP sees this as not true. Then because of that, PHP ignores the line of code for the if statement. So, it will execute the code for the “else” part. It doesn’t need to do any testing because else means “when all other condition is false or not true, run the code between the else curly brackets.“ For us,that was this:

else {
print ("This is a wrong person...next!");
}

So,how about if we got a lot of set of condition?do not worry  the PHP script already have the solution to solve this twisted question!haha

ok,in PHP,we can use this code for a lot of conditions:

if (first condition) 
{
//PHP run this code if
first condition is true
}
 

else if (second condition){
//PHP run this code if second condition is true
}
 

else 
{
//PHP run this code if first condition and second condition or all conditions above is false
}

is it easy right?hahaha,i hope so,but feel free if u got some problems

adios!!
N/NOTE:next post,we will deal with how-to-show-the-images-with-PHP-!


No comments:

Post a Comment