Many times a programmer is given an algebraic formula, and must write an assignment statement that
will correctly calculate the answer. After writing a statement, check it by hand, using sample values and
the method shown above to verify that the equation is correct. Use the following guidelines to convert
algebraic expressions to programming statements:
| Algebra | Program | Explanation: |
| XY | $x*$y | Algebra uses single letters for variables, and can omit the *, programming languages always require an operator. |
| X·Y | $x*y | Algebra sometimes uses a dot for multiplication. |
| 2X | $x*2 | Programming languages always require an operator. |
| $x*$x | There is no way to type a smaller exponent. | |
| pow($x,$n) | PHP has lots of built in functions, check the manual! | |
| sqrt($x) | another built in function | |
| ($a+$b)/$c | Compound numerators and divisors must be enclosed in parenthesis: Notice the difference between this example and the next one. | |
| $a+$b/$c |
Remember: Algebra uses single letters for variables: programmers should use good variable
names wherever possible!
Note: While you are looking at all of built in functions, take a look at the rand function, then go to the test page to roll dice.
rand returns a random integer number. If we find the remainder when dividing by 6 we get a value from 0 to 5. Add 1 to get values from 1 to 6.