Posted by tutor | Posted in PHP Tutorial | Posted on 09-08-2010-05-2008
0
The If Statement
This statement executes a piece of code only when a condition is true.
Syntax
if (condition) code to be executed if condition is true;
The if…else Statement
This is a two way condition evaluation, in which there are yes and no both lead to different code block.
We just add one else condition in the above if statement.
Syntax
if (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
If more than one line should be executed, then the lines should be enclosed within curly braces:
<html>
<body>
<?php
$d=date(“D”);
if ($d==”Fri”)
{
echo “Hello!<br />”;
echo “Today is Friday!”;
}
?>
</body>
</html>
The if…elseif….else Statement
This construct is used when there are more conditions than just true or false.
When PHP evaluates If…elseif…else statement it first sees if the If statement is true. If that test comes out to be false it then checks the first elseif statement.
If then also the result comes out to be false it will check the next elseif statement, if there exists any more otherwise, it will evaluate the else segment, if one exists.
Syntax
if (condition)
code to be executed if condition is true;
elseif (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
Posted by tutor | Posted in PHP Tutorial | Posted on 07-08-2010-05-2008
0
We have seen that If…elseif…else statement can be used where there are multiple conditions, but if the conditions are 20 or 30, then this number of if and elseif will look very clumsy. This may even decrease the code readability.
The other way round to solve this problem is to use the switch statement. As the name implies, it is a switch kind of construct, which switches between which code to execute, depending upon the value of switch variable.
Switch statement takes a single variable as input and then checks it against all the different cases you set up for that switch statement.
The PHP Switch Statement
Use the switch statement to select one of many blocks of code to be executed.
Syntax
switch (n)
{
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
default:
code to be executed if n is different from both label1 and label2;
}
First we have a single expression or a variable n, which is evaluated once. The value of the expression is then compared with the values for each case in the structure. If the expression value matches with a case value, the block of code associated with that case is executed.
At the end of each code block of every case ther is break statement, this break statement says that the condition is fulfilled so break out of the switch case. This prevents the code from running into the next case automatically.
After all the cases have been defined, we define a default case. This default case, as its name implies, is a case which is executed if the expression value matches with no other case value.
You can print a generic statement saying wrong choice or no matching values so that the user may know that a wrong input is given.
Posted by tutor | Posted in PHP Tutorial | Posted on 04-08-2010-05-2008
0
Conditional statements are used to evaluate some conditions and perform different actions based on the outcome.
You can use conditional statements in your code to do this.
PHP provides the following conditional statements:
- If statement – execute the piece of code only if a specified condition is true.if (condition) code to be executed if condition is true;
- If…else statement – this statement executes a two way condition, where the true part does something else and the false part executes something else.if (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
- If…elseif….else statement – if there are multi-way condition, where there are several blocks of code to be executed based on different conditions.if (condition)
code to be executed if condition is true;
elseif (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
- Switch statement – this statement is used to select one of many blocks of code to be executedswitch (n)
{
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
default:
code to be executed if n is different from both label1 and label2;
}