Operators, Flow Control, Arrays & Strings
Operators, Flow Control, Arrays & Strings
1
Expressions:
Operands:
An operand is one of the entities being manipulated in an expression.
Operators:
An operator is a symbol that specifies a particular action in an expression.
Operator Precedence
Operator precedence is a characteristic of operators that determines the order in
which they will evaluate the operands surrounding them.
$total_cost = $cost + $cost * 0.06;
Operator Associativity:
The associativity characteristic of an operator is a specification of how operations
of the same precedence (having the same precedence value as displayed in
Table 3-1) are evaluated as they are executed.
$value = 3 * 4 * 5 * 7 * 2;
2
Arithmetic Operators:
Examples:
Logical Operators:
Comparison Operators:
5
Control Structures:
if
The if statement is a type of selection statement that evaluates an expression and
will (or will not) execute a block of code based on the truth or falsehood of the
expression.
Syntax:
if (expression) if (expression) :
{ statement block
statement block
} endif;
If-else
Syntax:
if (expression) if (expression) :
{ statement block
statement block else :
} statement block
Else endif;
{
statement block
}
6
$flag = 1;
if ($flag == TRUE) :
print "The flag is true!";
else :
print "The flag is false !";
endif;
OR
$flag = TRUE;
// this implicitly asks "if ($flag == TRUE)"
if ($flag) :
print "The flag is true!";
else :
print "The flag is false!";
endif;
7
do..while:
do :
statement block
while (expression);
Example:
$n = 5;
$ncopy = $n;
$factorial = 1; // set initial factorial value
do
{
$factorial = $n * $factorial;
$n—;
}
while ($n > 0);
print "The factorial of $ncopy is $factorial.";
for:
for (initialization; condition; increment)
{
statement block
}
8
Example:
What if ??
$x = 5;
for (; ; $x += 2) :
print " $x ";
if ($x == 15) :
break; // break out of this for loop
endif;
endfor;
10
foreach:
There are two general forms of the foreach statement, each having its own
specific purpose:
11
$menu = array(“AIT", “ADT", “XYZ");
foreach ($menu as $item)
{
print "$item <BR>";
}
$inventory = array {
“Pepsi" => 15,
“Maza" => 17,
“Slice" => 32
}
foreach ($inventory as $i => $item_count)
{
print "$item_count bottles of $i remaining<BR>";
}
12
Switch:
The switch statement functions much like an if statement, testing an expression
value against a list of potential matches.
switch (expression)
{
case (condition) :
statement block
case (condition) :
statement block
...
default :
statement block
}
13
Example:
switch ($user_input) :
case("search") :
print "Let's perform a search!";
break;
case("dictionary") :
print "What word would you like to look up?";
break;
case("recipes") :
print "Here is a list of recipes…";
break;
default:
print "Here is the menu…";
break;
endswitch;
14
break:
endif;
print "val is $val <br>";
endforeach;
15
The lack of a break statement in a case will cause all subsequent commands in
the switch statement to be executed until either a break statement is found or the
end of the switch construct is reached.
$value = 0.4;
switch ($value) :
case (0.4) :
print "value is 0.4<br>";
case (0.6) :
print "value is 0.6<br>";
break;
case (0.3) :
print "value is 0.3<br>";
break;
default :
print "You didn't choose a value!";
break;
endswitch;
16
$boundary = 558;
$prime_counter++;
endfor;
17