learn

Numbers and mathematical operators

category: Basics
Created by: Dr.Ferrous

Numbers and mathematical operators

category: Basics
Created by: Dr.Ferrous
twitter google+ facebook pin 

PHP has both integer and float (decimal) number types. These two types can be classified under the generic title numbers.
Valid numbers can be anything like:   8,   258,   -37,   89.25,   -3.5785,   5.5e2

Number-type values are never quoted. The numbers added within quotes are strings with numeric values.
PHP supports a maximum integer of around two billion on most platforms (UNIX and Windows). With numbers larger than that, PHP will automatically use a floating-point type.

Numbers and Operator

The mathematical operators are specific symbols that performs mathematical calculations.
Operators act on the variables and numbers presented in PHP instructions.

The standard mathematical (Arithmetic) operators

Addition ( + )

Subtraction ( - )

Multiplication ( * )

Division ( / )

Modulus (division remainder) ( % )

Increment ( ++ )

Decrement ( -- )

Assignment Operators

( = )

( += )

( -= )

( *= )

( /= )

( .= )

( %= )

The mathematical calculations in PHP, as in arithmetic, are taken into account the operators precedence (the order in which complex calculations are made). If you want to force the execution order, you must group clauses in parentheses. Check the fallowing example:

PHP mathematical functions

Along with the arithmetic operators you can use PHP mathematical functions for more complex operations.
Here are some examples of functions:

abs(x) - The absolute value of "x"

ceil(x) - "x" rounded up to the next highest integer

floor(x) - "x" rounded to the next lowest integer

max(x, y, ...) - returns the highest of the parameter values

min(x, y, ...) - returns the lowest of the parameter values

number_format(x, d) - Turns a number into the more commonly written version, grouped into thousands using commas. Also, can set a specified number of decimal points, specified by "d" parameter:

pow(x, n) - "x" raised to the power of "n"

sqrt(x) - The square root of "x" or the special value NAN for negative numbers