Numbers

Previous Page
Next Page

2. Numbers

Being a general-purpose language, PHP has the basic set of mathematical operators and a basic set of mathematical functions. A full list can be found at http://php.net/math. Though it is beyond of the scope of this book, PHP also comes with an extension, BCMath, http://php.net/bc, to handle arbitrary precision operations.

As with strings, nearly all types of applications require some use of the math functions. Hence, this chapter focuses on those examples that demonstrate "math-centric" issues only, leaving examples based on other topics to those chapters so devoted.

Quick Hits

  • Find the absolute value of a number:

    $absvalue = abs($number);

    If the number is negative, the value returned is positive. If the number is positive, this function simply returns the number.

    Full documentation: http://php.net/abs

  • Find the integer remainder of a division:

    $remainder = $dividend % $divisor;

    Called the modulus operator, the integer remainder resulting from dividing the $dividend by the $divisor is returned.

    Full documentation: http://php.net/language.operators.arithmetic

  • Raise a number to a power:

    $result = pow($number, $power);

    Unlike other languages, PHP does not have a power operator. This function must be used instead.

    Full documentation: http://php.net/pow

  • Find the square root of a number:

    $root = sqrt($number);

    As true with the mathematical operation, one cannot take a square root of a negative value.

    Full documentation: http://php.net/sqrt

  • Round a fractional number up to the next highest whole number:

    $ceilinged = ceil($number);

    This function, if necessary, rounds a fractional number up to the next integer. The rounding occurs for any fractional value.

    Full documentation: http://php.net/ceil

  • Round a fractional number down to the next lowest whole number:

    $floored = floor($number);

    This function performs essentially the same operation as casting a float value as type integer, removing the decimal portion of the number. However, the value returned is still type float.

    Full documentation: http://php.net/floor

  • Round a number to the nearest whole number:

    $rounded = round($number);

    This function rounds to the nearest whole number. If the fractional part is less than 0.5, the result is the next lowest whole number. If greater than or equal to 0.5, the result returned is the next highest whole number.

    Full documentation: http://php.net/round

  • Find the largest or smallest value in an array:

    $maximum = max($array); $minimum = min($array);

    Return, respectively, the maximum and minimum value of an array of numbers.

    Full documentation: http://php.net/max and http://php.net/min

  • Convert a number in one base system to another:

    $newnumber = base_convert($number, $oldbase, $newbase);

    This function converts a number in any base, up to base 36, to a number in any other base, again up to base 36.

    Full documentation: http://php.net/base_convert

  • Generate a random integer:

    $result = rand($min, $max);

    This function returns an integer between $min and $max, inclusive.

    Full documentation: http://php.net/rand

  • Calculate an exponent:

    $result = exp($power);

    This function returns the natural base "e" raised to the specified power.

    Full documentation: http://php.net/exp

  • Calculate a logarithm:

    $result = log($number, $base);

    This function returns the logarithm of the specified number to the base provided. If the base is left off, it performs a natural logarithm.

    Full documentation: http://php.net/log


Previous Page
Next Page