PHP
Operators, Flow Control,
Arrays & Strings.
SAGAR REPALE
1
Expressions:
An expression is essentially a phrase representing a particular action in a program.
All expressions consists of at least one operand and one or more operators.
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:
EXAMPLE LABEL OUTCOME
$a + $b Addition Sum of $a and $b
$a - $b Subtraction Difference of $a and $b
$a * $b Multiplication Product of $a and $b
$a / $b Division Quotient of $a and $b
$a % $b Modulus Remainder of $a / $b
Assignment Operators:
EXAMPLE LABEL OUTCOME
$a = 5; Assignment $a equals 5
$a += 5; Addition-assignment $a equals $a plus 5
$a *= 5; Multiplication-assignment $a equals $a multiplied by 5
$a /= 5; Division-assignment $a equals $a divided by 5
$a .= 5; Concatenation-assignment $a equals $a concatenated with 5
String Operators:
EXAMPLE LABEL OUTCOME
$a = “abc”.“def”; Concatenation $a equals the concatenation of the
two strings $a and $b
$a .= “ghijkl”; Concatenation-assignment $a equals its current value
3
concatenated with “ghijkl”.
Autoincrement and Autodecrement Operators:
EXAMPLE LABEL OUTCOME
++$a, $a++ Autoincrement Increment $a by 1
—$a, $a— Autodecrement Decrement $a by 1
Examples:
$inventory = 15; // Assign integer value 15 to $inventory
$old_inv = $inventory—; // FIRST assign $old_inv the value of
// $inventory, THEN decrement $inventory.
$orig_inventory = ++$inventory; // FIRST increment inventory, then assign
// the newly incremented $inventory value
// to $orig_inventory.
Logical Operators:
EXAMPLE LABEL OUTCOME
$a && $b And True if both $a and $b are true.
$a AND $b And True if both $a and $b are true.
$a || $b Or True if either $a or $b are true.
$a OR $b Or True if either $a or $b are true.
! $a Not True if $a is not true.
NOT $a Not True if $a is not true.
$a XOR $b Exclusive or True if only $a or only $b is true.
4
Equality Operators:
EXAMPLE LABEL OUTCOME
$a == $b Is equal to True if $a and $b are equivalent.
$a != $b Is not equal to True if $a is not equal to $b
$a === $b Is identical to True if $a and $b are equivalent
and $a and $b have the same
type.
Comparison Operators:
EXAMPLE LABEL OUTCOME
$a < $b Less than True if $a is less than $b
$a > $b Greater than True if $a is greater than $b
$a <= $b Less than or equal to True if $a is less than or equal to
$b
$a >= $b Greater than or equal to True if $a is greater than or equal
to $b
($a == 12) ? 5 : -1 Trinary If $a equals 12, then the return
value is 5. Otherwise, the return
value is –1.
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:
for ($i = 10; $i <= 100; $i+=10) :
print “i=$i <br>";
endfor;
What if ??
for ($i = 10; $i <= 100; print "\$i = $i <br>", $i+=10) ;
for for U :Slove it!
for ($x=0,$y=0; $x+$y<10; $x++) :
$y +=2;
print "\$y = $y <BR>";
$sum = $x + $y;
print "\$sum = $sum<BR>";
endfor; $y = 2
$sum = 2
$y = 4
$sum = 5
$y = 6
$sum = 8
$y = 8
9
$sum = 11
It is also possible to omit one of the components of the conditional expression.
$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:
foreach (array_expression as $value)
{
statement
}
foreach (array_expression as $key => $value)
{
statement
}
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>";
}
In the above example, two points are worth noting. ???
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:
$user_input = "recipes"; // assume $user_input is passed in to the script
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:
$arr = array(14, 12, 128, 34, 5);
$magic_number = 128;
foreach ($arr as $val) :
if ($val == $magic_number) :
print "The magic number is in the array!";
endif;
print "val is $val <br>";
endforeach;
What will be the Output??
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;
for ($i = 0; $i <= $boundary; $i++) :
if ( ! is_prime($i)) :
continue;
endif;
$prime_counter++;
endfor;
17