Hands-On-Exercise 3 Expressions, Operators and Conditional

advertisement
Hands-On-Exercise 3 Expressions, Operators and
Conditional Statements
This hands-on-exercise takes a look at the use of expressions, operators and
conditional statements in php. These are the building blocks of php.
1. We’ve already seen, in HOE 2, how to assign values to variables:
$a = 'A String';
$b = "Another String";
$c = 5;
$d = "A String containing interpreted variables: $c and
$b";
$e = 'A String where the variable isn\'t interpreted ($c)
and an escape character had to be used.';
etc.
You can test the operation of any of the above if you wish.
2. Performing calculations in PHP is easy with the use of the arithmetic
operators. Set $a = 8 and $b = 14 and then perform and echo the
following operations (you have two choices here: you can set a new variable
to the result of the operation and then echo that or you can directly echo the
operation).
$a + $b
$a - $b
($b - $a) * $a
$b % $a
$b / $a
Check the results make sense! Notice the use of parentheses in the third
example to ensure the calculations are carried out in the order we require
(PHP follows the standard mathematical precedence rules)
3. Keeping your two variables set as in 2. and also set:
$c = 'This '
$d = 'class '
Now echo the results of the following:
BBK ITApps – Web Programming using PHP: HOE 3
Page 1 of 4
$a += $b
$a -= $b
$a = $a.$b
$c .= $d
Do the results make sense? (remember that you are updating the value of $a
as the code progresses. Note that the concatenation operator is the same as
one of the options used when echoing in HOE 2.
4. You will end up using comparison operators quite frequently as they are often
the basis for whether a piece of code (e.g. a loop) is executed or not.
Remember: The results of comparison operators are TRUE or FALSE. Echoing a true value will result in a ‘1’. Echoing a false value will result in an empty
string (i.e. you will see nothing!).
Set the following variables and try the comparisons, echo the results on
separate lines:
$a = 3
$b = 4
$c = '4'
$d = 'My String'
$e = 'Not My String'
$a != $b
$a == $b
$b == $c
$b === $c
$a < $b
$e > $d
($c - $b) != 0
5. If we now take a look at incrementing/decrementing values we can see the
difference between pre- and post-incrementing. Try the following:
$a = 5;
$b = 6;
echo 'Pre-increment $a: '.++$a.'<br />';
echo 'Post-increment $b: '.$b++.'<br />';
echo "Check current values, \$a = $a and \$b = $b<br />";
BBK ITApps – Web Programming using PHP: HOE 3
Page 2 of 4
6. To test out the logical operators set the following variables:
$a = 5
$b = 8
$c = 10
Now echo the results of the following:
($a < $b) AND ($c > $a)
($c > $b) XOR ($b > $a)
!($a > $c)
!($a < $c)
($c > $a) || ($a > $b)
7. Let’s recall the syntax for the if … elseif … else control structure.
if (expression) {
statements;
} elseif (expression) {
statements;
} else {
statements;
}
Set two variables, $a and $b, initially to the same value (of your choice). First,
test to see if $a is greater than $b and echo a message to say as much. Then
check to see if $a is less than $b and again echo an appropriate message.
Finally, if neither are TRUE echo a message to say that $a is equal to $b. By
changing the values of your variables, $a and $b, check the operation of your
if statement.
8. Write a switch statement that checks a variable for the names Stuart or Fred
and displays a message welcoming them or if the variable is neither Stuart
nor Fred displays a message saying 'Welcome Stranger'.
Syntax Reminder:
switch (expression) {
case (result1):
statements;
break;
BBK ITApps – Web Programming using PHP: HOE 3
Page 3 of 4
case (result2):
statements;
break;
default:
statements;
}
Advanced Exercise
Let’s recall the ternary operator. This returns a result depending on whether the
expression tested is either TRUE or FALSE.
A reminder of the syntax:
(expr) ? returned_if_expr_true:returned_if_expr_false
Set two variables, one should be to 25 and the other to an age of your choice. Use
the ternary operator to compare the age with the variable containing 25 and echo
suitable responses (e.g. The age input is greater than 25 or The age is not 25)
depending on the result.
BBK ITApps – Web Programming using PHP: HOE 3
Page 4 of 4
Download