CS1109 Programming and Web Development, 2012-2013

© Derek Bridge 2012

University College Cork

Aims:

- to learn about expressions in PHP
- to learn about precedence and associativity

- A
**data type**is defined by giving a**set of values**, and the**operations**we can use to create, access and modify the values - String operators
- Concatenation (.) and some others

- Numeric operators
- Addition (+), subtraction (-), multiplication (*), division (/), modulus, i.e. remainder (%), and some others

- An expression combines operators and data (operands), and can be evaluated to give a result
- E.g.
`2 + 3`

`7 % 2`

`$age + 1`

- But there can be more than one operator in an expression, e.g.
`1 + 2 + 3`

`3 * 4 * 2`

- And there can be sub-expressions within parentheses, e.g.
`3 * (4 + 2)`

`(1 + 5) * (3 * 2)`

`3 * (4 + (2 * 6))`

- If there are multiple operators in an expression, in what order will they be evaluated?
**Class exercise:**Evaluate each of these in two different orders:- 2 + 3 * 4
- 8 / 2 - 1
- 5 - 2 + 3
- 18 / 6 / 3

- The order PHP evaluates the operators depends on their
**precedence**and**associativity** **Precedence:**- PHP evaluates higher precedence operators ahead of lower precedence ones
- *, /, % have higher precedence than +, -

**Associativity:**- what to do in the event of a tie in precedence
- +, -, *, /, % are
**left-associative** - left-associative means evaluate the equal precedence operators from left-to-right

**Class exercise:**Hence, evaluate each of these:- 2 + 3 * 4
- 8 / 2 - 1
- 5 - 2 + 3
- 18 / 6 / 3

- You can force any ordering you want by using parentheses, which raise the precedence of whatever is within them
**Class exercise:**Evaluate each of these:- (2 + 3) * 4
- 2 + (3 * 4)
- (8 / 2) - 1
- (5 - 2 + 3)
- 18 / (6 / 3)

- The following formula converts a temperature from degree centigrade (Celsius) to degrees Fahrenheit:
- In a PHP program, suppose
`$temp_in_centigrade`

contains a temperature in degrees centigrade - Does this assignment statement need any parentheses to correctly implement
the above formula?
$temp_in_fahrenheit = 32 + $temp_in_centigrade * 9 / 5;

- The following formula calculates the average of
two numbers
`x`and`y`: - In a PHP program, suppose
`$age_of_mother`

and`$age_of_father`

contain the ages of the user's parents - Complete this assignment statement to calculate the average age of the user's
parents
$avg_age_of_parents = _____________________________________________________

- In a PHP program,
`$prev_mark`

contains a student's mark for the previous CS1109 assignments - And
`$new_mark`

contains his/her mark for a new assignment - Complete this assignment statement to calculate the difference between the
new one and the previous one as a percentage of the previous one:
$percentage_change = __________________________________________________

- In Computer Science, there are many ways to specify colours, e.g. by name, by RGB value, by HSV value
- Even with RGB values, we can specify the amounts of red, green and blue
- as percentages, e.g.
`rgb(70%,0%,70%)`

- as decimal numbers between 0 and 255 inc., e.g.
`rgb(0,255,0)`

- as hexadecimal numbers, e.g.
`#00FF00`

- as percentages, e.g.
- Suppose
`rgb_form.html`

contains this form:<form action="rgb_convert.php" method="get"> <input type="text" name="r_per_cent" value="0" /> <input type="text" name="g_per_cent" value="0" /> <input type="text" name="b_per_cent" value="0" /> <input type="submit" /> </form>

- Write
`rgb_convert.php`

to convert the user's RGB value expressed as percentages to an RGB value expressed as decimal integers between 0 and 255 inclusive