Difference between revisions of "PHP105"
m (→Exercise 3) |
|||
Line 1: | Line 1: | ||
− | == | + | == Expressions, Operators, and Variable Assignments == |
=== Exercise 1 === | === Exercise 1 === | ||
Line 64: | Line 64: | ||
PHP contains a library of mathematical functions which are available to you. These are well documented elsewhere, and you are encouraged to research this topic. | PHP contains a library of mathematical functions which are available to you. These are well documented elsewhere, and you are encouraged to research this topic. | ||
− | |||
== Operator Precedence == | == Operator Precedence == |
Revision as of 16:36, 25 January 2007
Expressions, Operators, and Variable Assignments
Exercise 1
Type the following code as part of a web page. Experiment with changing values and operations until you fully understand how to manipulate variables in this exercise.
<? $num = 1 echo 'num is now: $num<BR>'; $num = 4 + 9; echo 'num is now: $num<BR>'; /* ** all the following add one to the value of $num */ $num = $num + 1; echo 'num is now: $num<BR>'; $num += 1; echo 'num is now: $num<BR>'; $num++; echo 'num is now: $num<BR>'; /* ** these subtract one from $num */ $num = $num -1; echo 'num is now: $num<BR>'; $num -= 1; echo 'num is now: $num<BR>'; $num--; echo 'num is now: $num<BR>'; /* ** double the value of $num */ $num = 23; echo 'num is now: $num<BR>'; $num = $num * 2; echo 'num is now: $num<BR>'; $num *= 2; echo 'num is now: $num<BR>'; /* ** halve the value of $num */ $num = 100; echo 'num is now: $num<BR>'; $num = $num / 2; echo 'num is now: $num<BR>'; $num /= 2; echo 'num is now: $num<BR>'; /* ** A combination */ $num = 8; echo 'num is now: $num<BR>'; $num = (((($num + 4) * 2) - 6) / 3); echo 'num is now: $num<BR>'; ?>
Exercise 2
- Write a PHP script which assigns the first three prime numbers to the variables $prime1, $prime2 and $prime3.
- Write a calculation to determine the total (addition) of these three numbers
- Write a calculation to determine the sum (multiplication) of the three numbers
- Suitable lines of code should output the results in this type of manner:
The total of <first number> + <second number> + <third number> = <answer>
PHP contains a library of mathematical functions which are available to you. These are well documented elsewhere, and you are encouraged to research this topic.
Operator Precedence
When a mixed expression is presented to the PHP interpreter, the order in which the calculation is to be performed must be determined - unless parentheses (brackets) clarify the intended order. Without parentheses the expression is evaluated according to the 'order of precedence' – which amongst other things states that multiplication and division occur before addition and subtraction.
For example, what is the value of $num when this expression has been evaluated:
$num = 2 * 3 + 4;
Is it 10 ? (2 times 3 is 6, plus 4 is 10). Is it 14 ? ( twice 7 – the 3 plus 4)
The rules of precedence can be confusing at first. Therefore it is strongly suggested that you use parentheses, eg
<? // this produces an answer of 10 $num = (2 * 3) + 4; echo $num."<BR>"; //this produces an answer of 14 $num = 2 * (3 + 4); echo $num."<BR>"; ?>
Type Conversion
At times you may wish to convert a variable from one type to another. An example might be if someone's age is stored as "23". This is a string of characters. If you wanted to do any calculations with it, you will have to convert it in to an Integer.
PHP offers functions that do this for you, as well as some automatic type conversion.
- A string variable = strval(a mixed variable)
- An integer variable = intval(a mixed variable)
- A float variable = floatval(a mixed variable)
eg if a variable called $age contains the string value "23", and you want to calculate how many years there are before that person reaches 40, the code would use the second of the listed functions above, e.g.
<? // assigns the string value to $age $age = "23"; //convert the string to an Integer $int_age = intval($age); $years_to_go = 40 - $int_age; echo "The years to go before forty is $years_to_go"; ?>
This produces:
The years to go before forty is 17
Automatic Type Conversion
This occurs when variables of two or more types are mixed in an expression, with one of the variables being used as though it were of a different type. Under these circumstances, PHP will automatically convert the variable. The conversion follows set built-in rules, therefore it is best to ensure that you have clarity in all expressions you write, or that you write code to do the conversion for you.
Type the following PHP code in to a web page. Predict what you think the output will be, and insert appropriate 'print' or 'echo' statements to display the results on your page. Be careful, the second three type conversions are not so obvious.
<? // $num is set as an integer = 115 $num = "100" + 15; // $num is set as a float = 115.5 $num = "100" + 15.5; // $title is set as a string = "39 steps" $title = 39 . " steps"; // $num is set as an integer = 39 $num = 39 + " steps"; // $num is an integer = 42 $num = 40 + "2 blind mice"; // $num is a float, but what does it mean ? $num = "test" * 4 + 3.14159; ?>
Ensure you put some line breaks in to your code – to improve appearance.
The code produces:
115 115.5 39 steps 39 42 3.14159
Exercise 3
Create a number of variables, assigning them with your personal details, as though these had been entered on a form – name, various fields of your address, post code, phone number, etc. Using a web page, and as much PHP as possible, output these details, using appropriate text formatting.