Friday, 2 September 2011

Introduction to PHP: If…Else Statement


One of the most common PHP language constructs that you will encounter is the if…then statement. The if…then statement allows you to evaluate an expression and then, depending if it is true or not, take a course of action. For example:


$x = 1;
if($x) {
echo "True!";
}
Since $x = 1, then PHP interprets it in the context of the if statement to be a boolean type. Since 1 = true, the if statements prints out the message. Literally you can read it as "If True, then print out "True!".
Another example, but this time with an "else" clause:
$a = 5;
$b = "10";
if($a > $b) {
echo "$a is greater than $b";
} else {
echo "$a is not greater than $b";
}

PHP doesn't care that $a is an integer and $b is a string. It recognizes that $b also makes a pretty good integer as well. It then evaluates the expression, "If $a is greater than $b then print out that $a is greater than $b; if not (else) then print out $a is not greater than $b." It's also important to note that you don't say, "$a is less than $b," since it is possible that $a could be equal to $b. If that is the case, then the second part of the expression, the else statement, is the part that runs.
Note the use of brackets to enclose the actions. Also note the lack of semicolons where the brackets are used.
One final example is the if…elseif…else statement:
if($a == $b) {
// do something
} elseif ($a > $b) {
// do something else
} elseif($a < $b) {
// do yet something else
} else {
// if nothing else we do this...
}
Switch Statement:
The Switch statement is used to perform one of several different actions based on one of several different conditions. Switch statements allow for additional evaluations of the data, even though one of the cases may have been met. Switch statements can also save you from typing many if…elseif…elseif… statements.

Syntax:

switch (expression)

{

case label1:

Statement(s);

break;

case label2:

Statement(s);

break;

default:

Statement(s);

}


Example:

$a = "100";
switch($a) {
case(10):
echo "The value is 10";
break;
case (100):
echo "The value is 100<br>";
case (1000):
echo "The value is 1000";
break;
default:
echo "<p>Wrong Input";
}
switch statements have four basic parts:
  1. The switch— The switch identifies what value or expression is going to be evaluated in each of the cases. In the example above, you tell the switch statement that you are going to evaluate the variable $a in the subsequent cases.
  2. The case— The case statement evaluates the variable you passed from the switch command. You can use case() the same way you'd use an if statement. If the case holds true, then everything after the case statement is executed, until the parser encounters a break command, at which point execution stops and the switch is exited.
  3. Breakpoints— Defined by the break command, exit the parser from the switch. Breakpoints are normally put after statements executed when a case() is met.
  4. Default— The default is a special kind of case. It is executed if none of the other case statements in the switch have been executed.





No comments:

Post a Comment