Web App Prog CH4
Web App Prog CH4
Web App Prog CH4
4
[Please insert any
relevant photo Comparison Operator to
around this box] create the Statement
PHP scripts are a series of instruction in a file. PHP begins at the top of the file and executes
each instruction in order. Sometimes you want your statement to execute in a different
order, such as executing a statement only when certain conditions exist or executing a
statement more than once. PHP provides several complex statements that change the
order of execution in your script.
Conditional Statement
Conditional statements execute a block of statements only when certain conditions are
met. The most common conditional statement is the if statement, which test a condition
and executes a block of code when the condition is true. An if statement can also have
33 | P a g e C h a p t e r 4 : C o n t r o l l i n g T h e F l o w o f t h e S c r i p t
else if and else code blocks. The else if sections test different condition. The else block
executes if none of the condition tested by the if and else if block are true. Another
useful conditional statement is the switch statement, which defines a series of conditions.
Each block of code executes when a specific condition is true, such as when $var1 equals
1. PHP executes only the block of code for which the conditions is true. For example PHP
may test a variable called $weather. If the value in weather is “raining”, one block of code
executes; if the value in weather is “sunny”, a different block of code executes and if the
value is weather is “snowing” a third block of code executes.
Conditions
Conditions are expressions that PHP test or evaluate to determine whether they are true or
false. Conditions are used in both conditional statement and loops to determine whether a
block of statement should be executed. The set up conditions you compare values. You
can compare values using comparison operators that test whether two expressions are
equal, one is higher than another or one is lower than other. For example, the == operator
means that two expressions are equal. A condition such as $var1 == $var2 is true if the
values in the variable are equal and is false if the values are not equal. You can also set up
pattern, called regular expression and test whether a string matches the pattern. For
example, you may want to identify strings that begin with T or string that have numbers in
them.
Loops
Loops are statement that executes a block of statement repeatedly. A loop can repeat a
specified number of times or only as long as certain conditions exist. A for loop sets up a
counter and repeat a block of statement until the counter reaches a specified number. The
for loop is very flexible, enabling you to set up complex loops. The while and do-while
statement as long as a certain condition is true. The statement block stops repeating when
the condition is no longer true. The while and do-while statement are very similar, differing
only in the location in the loop where the condition is tested.
Conditions are expressions that PHP test or evaluate to see whether they are true or false.
Conditions are used in conditional statements to determine whether to execute a block of
34 | P a g e C h a p t e r 4 : C o n t r o l l i n g T h e F l o w o f t h e S c r i p t
statement. To set up conditions, you often compare values using the following comparison
operators:
Often you need to set up more than one comparison with one of the logical operators –
AND, OR, or XOR. The logical operators evaluate each comparison to obtain the Boolean
value for each expression and then compare the Boolean values. The AND operator returns
TRUE only if both comparisons are true, the OR operator returns TRUE if one but not both,
comparison is true. For some test evaluating the comparison on the left is sufficient. For
example, if the first comparison evaluates to FALSE, then AND must evaluates to FALSE. At
this point, AND stops and the second comparison is not evaluated. This process is called
short circuiting.
35 | P a g e C h a p t e r 4 : C o n t r o l l i n g T h e F l o w o f t h e S c r i p t
The logical operator like any other operator is executed in order of precedence. The order
is AND first , XOR second and OR third. Consequently all AND are executed before all ORS.
Within precedence order the logical operator are executed from left to right. You can use
&& and an alternative for AND and || for OR. You can change the order in which logical
operator are executed with parentheses. The logical operator inside the parentheses is
evaluated first.
A code block for the if and else if executes when the condition is true. An if statement
must contain one, and only one, if section. It can optionally contain one or more else if
sections and one else section. The script tests the if condition first. If the condition is true,
the code block executes and the script moves to the next statement following the if
statement or if there are no more conditions to test and no else section, to the next
statement following the if statement. The script continue to test conditions until it
encounter a condition that is true or until it reaches an else section or the end of the if
statement.
36 | P a g e C h a p t e r 4 : C o n t r o l l i n g T h e F l o w o f t h e S c r i p t
4.5 Using a Switch Statement
Usually the if conditional statement is most appropriate. However sometimes you want to
set up a list of conditions with a block of statement for each condition. You can use the
switch statement to do so. The switch statement tests the value of one variable and
executes the block of statement for the matching value of the variable. The general format
is:
Switch ($variablename)
{
case value:
block of statement
break;
case value:
block of statement
break;
default:
block of statement
break;
}
The switch statement tests the value of $variablename and executes the appropriate block
of code. You can use as many case blocks as you need. The break statements are essential.
If a case block does not include a break statement, the script does not stop executing at
the end of the case block. You can also nest switch statement.
37 | P a g e C h a p t e r 4 : C o n t r o l l i n g T h e F l o w o f t h e S c r i p t
4.6 Using a For Loop
Loops are used frequently in scripts to set up a block of statement that repeats. The for loop
uses a counter to determine when the block of statements should stop repeating. The basic
for loop, the loop that is most frequently needed, has the following format:
for (startvalue; endcondition; increment)
{
block of statement
}
The parameters in the for statement can be values or variables or expressions as follows:
for ($i=$age; $i<=$age+20; $i++)
for($i=1; $<count($arrayname);$++)
The first parameter sets up the counter, giving it a variable name and a starting value. The
second parameter sets the ending value. As long as thus statement is true, the loop repeats,
when the statement is not true, the loop stops. The third parameter is an expression that
increments the counter. In the statement block, you can use the counter variable as you
use any other variable. You can nest for loop inside of for loop. Nesting is useful to process
table and multilevel list. You can build more advanced for loop by including more than one
expression are separated by a comma (,);
for($i=1, $j=0; $i<=$age+20; $i++, $j++)
This sets up two counters that you can use in your statement block.
Loops are used frequently in script to set up a block of statement that repeats. The while
loop is a simple loop that continues repeating as long as certain conditions are true. The
following is the general format of a while loop:
while(condition)
{
Block of statement
}
38 | P a g e C h a p t e r 4 : C o n t r o l l i n g T h e F l o w o f t h e S c r i p t
At the beginning of the while loop, the script checks the condition. If the condition is true,
the block of statements executes. At the end of the block of statement the script returns to
the beginning of the loop and check the condition again. The loop continues repeating
until the condition is not true at which point the script jumps to the statement after the while
loop.
If the condition at the top of the loop is not true when first tested, the while loop can
terminate without executing the block of statement even once. You can nest while loop
inside of while loops or other condition statement or loops. You can break out the statement
block of a while loop before reaching the end of the block with a break statement or a
continue statement.
Loop is used frequently in script to set up a block of statement that repeats. A do-while
statement is a simple loop, similar to a while loop. The loop continues repeating as long as
certain conditions are true. The following is a general format of a do-while loop:
do
{
block of statement
} while(condition);
The do-while loop checks the condition after the statement is executed. If the condition is
true, the loop returns to the beginning and repeat the loop executing the statement again.
The loop continues repeating until the condition is not true, at which point in the script does
not return to the top of the loop but instead leaves the loop, drooping to the statement
after the do-while loop.
39 | P a g e C h a p t e r 4 : C o n t r o l l i n g T h e F l o w o f t h e S c r i p t
Because the do-while loop tests the condition at the end of the loop, it will execute the
statement block at least once, even if the condition is not true when the loop begin.
Another loop, the while loop test, the block of statement may not executes at all. You can
nest do-while loop inside of the do-while loops or other condition statement or loops. You
can break out of the code block of the do-while loop before reaching the end of the block
with the break statement or a continue statement.
40 | P a g e C h a p t e r 4 : C o n t r o l l i n g T h e F l o w o f t h e S c r i p t