Peep in my mind

Tag Archive | "numbers"

Tags: , ,

Numbers

Posted on 11 June 2009 by admin

php101
Doing math in php

$variable = value * value
$variable = value + value
$variable = value – value
$variable = value / value

Formatting Numbers

Rounding numbers – rounds values to specified decimal places. It can be number or variables. There is an option that allow users to round too the number of decimal places.

Example:
round (6.20); = 6
round (6.369, 2); = 6.37
$number = 694.2783;
round ($number); = 694

Number format – works like round but adds commas in the to numbers.
number_format (6.2243, 2); = 6.22
number_format (69, 2); = 69.00
number_format (123456789); = 123,456,789

Precedence - the order in which calculations are made.

8 – 4 / 2 = 6 and not 2 because division comes first.

To avoid this use ()
(8 – 4) /2 ) = 2
8 – (4 / 2 ) = 6

Incrementing and decrementing a number add or subtracts 1 value
$var++; or $var–; this will give the value +1 or -1 effect.

Creating random numbers

rand()

$n = rand();- this generate random number
$n = rand(0,100); – this generate random number 0 to 100

Other types are getrandmax() – get height random number
mt_rand() – it suppose to work better then rand and great for cryptography.

These notes are from PHP for the World Wide Web, Third Edition (Visual QuickStart Guide) written by Larry Ullman. In order to make sense of my notes you should pruchase the PHP book its very well written and easy to follow.

Comments (0)

Advertise Here
Advertise Here

RELATED SITES