PHP I : Numbers

Creating a Calculator Form

Follow this lesson in Ullman Chapter 4. The scripts are located in the 04 directory.

See the page calculator.html described on Ullman pages 68ff.

See the page handle_calc.php described on Ullman pages 71ff.

Particularly note these lines:

// In case register_globals is disabled.
$price = $_POST[‘price’];

 

Formatting Numbers

round()

number_format()

printf()

sprintf()

 

Operators

Operator Purpose  
-$a
Negation Opposite of $a.
$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 divided by $b.
Combined Operators
Same as:
$a += $b
Addition $a = $a + $b
$a -= $b
Subtraction $a = $a – $b
$a *= $b
Multiplication $a = $a * $b
$a /= $b
Division $a = $a / $b
$a %= $b
Modulus $a = $a % $b
Incrementing/Decrementing  
a++
Increment $a = $a + 1
a–
Decrement $a = $a – 1

 

Random Numbers

See the page random.php.

rand()

getrandmax()

mt_rand()

ceil()

floor()

abs()

 

To do for this section:

Examine the different versions of handle_calc.php. What are the differences, and why are they significant?

Create your own page that demonstrates round() or number_format(). It should have text boxes that take a decimal number, and a number to round by.

 

To do out of class:

Review Chapter 4 of Ullman.