Saturday, July 11, 2009

php6-control structure

Control Structures
divided into two groups: conditional control structures and loop control structures. The conditional control structures affect the flow of the program and execute or skip certain code according to certain criteria, whereas loop control structures execute certain code an arbitrary number of times according to specified criteria
A.Conditional Control Structures
Conditional control structures are crucial in allowing your program to take different execution paths based on decisions it makes at runtime. PHP supports both the if and switch conditional control structures.
1.if statement
The PHP if statement tests to see if a value is true, and if it is a segment of code will be executed.
$my_name = "someguy";
if ( $my_name == "someguy" )
{
statement1;//the coding appear here
}

?>

2.if..else
if if part is correct than statement1 execute else statement2 will execute.
$number_three = 3;
if ( $number_three == 3 ) {
statement1;
}
else
{
statement2;
}
?>

3.if...elseif
When PHP evaluates your If...elseif...else statement it will first see if the If statement is true. If that tests comes out false it will then check the first elseif statement. If that is false it will either check the next elseif statement, or if there are no more elseif statements, it will evaluate the else segment, if one exists
$employee = "Bob";
if($employee == "Ms. Tanner")
{
echo "Hello Ma'am";
}
elseif($employee == "Bob")
{
echo "Good Morning Sir!";
}
else
{
echo "Morning";
}
?>

4.switch
The way the Switch statement works is it takes a single variable as input and then checks it against all the different cases you set up for that switch statement. Instead of having to check that variable one at a time, as it goes through a bunch of If Statements, the Switch statement only has to check one time.
$destination = "New York";
echo "Traveling to $destination
";
switch ($destination){
case "Las Vegas":
echo "Bring an extra $500";
break;
case "Amsterdam":
echo "Bring an open mind";
break;
case "Egypt":
echo "Bring 15 bottles of SPF 50 Sunscreen";
break;
case "Tokyo":
echo "Bring lots of money";
break;
case "Caribbean Islands":
echo "Bring a swimsuit";
break;
default:
echo "Bring lots of underwear!";
break;
}
?>

No comments:

Post a Comment