PHP Math Functions

  • bccomp — compare two numbers of arbitrary precision

    • bccomp ( string $left_operand , string $right_operand [, int $scale = int ] ) : int
    • Parameter explanation
      • left_operand, the left operand, is a string.
      • right_operand, the right operand, is a string.
      • the optional scale parameter is used to set the indicated number, i.e. the decimal part used in the comparison.
    • Returns 0 if the two numbers are equal, returns 1 if the left number left_operand is greater than the right number right_operand, otherwise returns -1.

    • Verified: if the argument is a string, no matter what the string is, the string is always 0. The arguments can be numbers for comparison.

  • bcdiv divides two numbers of arbitrary precision

    • bcdiv ( string $left_operand , string $right_operand [, int $scale = int ] ) : string
    • Divides the left operand by the right operand
    • Returns a result of type string; if the right operand is 0 the result is null

    • Verified: the division result is always rounded to an integer; unless the third parameter is specified, only the whole number is kept

  • bcmod — take the modulus of an arbitrary-precision number

    • bcmod ( string $left_operand , string $modulus ) : string
    • Uses the modulus on the left operand
    • Returns a string-typed result after the modulus; if the modulus is 0 it returns null
    • The modulus operation is actually = dividend - divisor x (integer value of dividend/divisor). For example, for 100%17, 100=175+15, so 100%17=15. Here 15 is 100-175. That is the result of the modulus 100%17.

      Verified: the result of the modulus is always a number with no decimal point

  • bcmul — multiply two numbers of arbitrary precision

    • bcmul ( string $left_operand , string $right_operand [, int $scale = int ] ) : string
    • Multiplies the left operand by the right operand
    • Returns a result of type string.

    • Verified:

      • If the sum of the first decimal digits of the two parameters is greater than or equal to 2 and less than 5, add 1 to the result value.
      • If the sum of the first decimal digits of the two parameters is greater than or equal to 5 and less than 8, add 2 to the result value.
      • If the sum of the first decimal digits of the two parameters is greater than or equal to 8 and less than or equal to 10, add 3 to the result value.
  • bcpow — raise an arbitrary-precision number to a power

    • bcpow ( string $left_operand , string $right_operand [, int $scale ] ) : string
    • Raises the left operand to the power of the right operand.
    • Verified: by default the operation keeps no decimal places; only when the third parameter specifies the number of decimal places kept are there decimals.
  • base_convert — convert a number between arbitrary bases

    • base_convert ( string $number , int $frombase , int $tobase ) : string
      • number the number to convert
      • frombase the current base
      • tobase the base to convert to
  • bindec — convert binary to decimal

    • decbin ( int $number ) : string
  • decbin — convert decimal to binary

    • decbin ( int $number ) : string
  • dechex — convert decimal to hexadecimal

    • dechex ( int $number ) : string
  • decoct — convert decimal to octal

    • decoct ( int $number ) : string
  • floor — round down (discard the fraction)

    • Note: if you round a negative number and the negative number has a decimal part with the first decimal digit greater than or equal to 1, add 1 to the result value
  • fmod — return the floating-point remainder of a division

    • fmod ( float $x , float $y ) : float
    • Returns the remainder of the division; the arguments may be integers, and the return value is always a float
  • hexdec — convert hexadecimal to decimal

    • hexdec ( string $hex_string ) : number
  • intdiv — round the result of a division to an integer

    • intdiv ( int $dividend , int $divisor ) : int
    • The quotient of dividend divided by divisor, rounded to an integer.
    • If divisor is 0, a DivisionByZeroError exception is thrown. If dividend is PHP_INT_MIN and divisor is -1, an ArithmeticError exception is thrown.
  • is_finite — determine whether a value is finite

    • is_finite ( float $val ) : bool
    • Returns TRUE if val is a legal finite value within the range allowed for PHP floats on this platform.
  • is_infinite — determine whether a value is infinite

    • Returns TRUE if val is infinite, otherwise returns FALSE.
  • is_nan — determine whether a value is a legal number

    • is_nan ( float $val ) : bool
    • Returns TRUE if val is not a number, otherwise returns FALSE.
    • Note: this one really confused me - this function actually returns true when it’s not a number, and false when it is a number
  • lcg_value — combined linear congruential generator

    • lcg_value ( void ) : float
    • A pseudo-random number in the range (0, 1).
  • round — round a float

    • Returns the result of rounding val according to the specified precision (the number of digits after the decimal point). precision can also be negative or zero (the default).
    • round ( float $val [, int $precision = 0 [, int $mode = PHP_ROUND_HALF_UP ]] ) : float
      • val the value to process
      • precision the optional number of digits after the decimal point.
      • mode one of the following: PHP_ROUND_HALF_UP, PHP_ROUND_HALF_DOWN, PHP_ROUND_HALF_EVEN or PHP_ROUND_HALF_ODD
  • ctype_alnum — check for alphanumeric characters

    • If all characters in text are letters and/or digits, returns TRUE, otherwise returns FALSE
  • ctype_alpha — check for letters only

    • If every character in text is a letter in the current locale, returns TRUE, otherwise returns FALSE.
  • ctype_cntrl — check for control characters

    • Control characters are, for example: newline, tab, space.
    • If every character in text is a control character in the current locale, returns TRUE; otherwise returns FALSE.
  • ctype_digit — check for digits only

    • If the text string is a decimal digit, returns TRUE; otherwise returns FALSE
  • ctype_graph — check for printable strings, excluding spaces

    • If every character in text is visibly printable (no whitespace), returns TRUE; otherwise returns FALSE.
  • ctype_lower — check for lowercase characters

    • If every character in text is a lowercase letter in the current locale, returns TRUE; otherwise returns FALSE.
  • ctype_print — check for printable characters

    • If every character in text can actually be output in the current locale (including whitespace), returns TRUE; if text contains control characters or strings that produce no output at all, returns FALSE.
  • ctype_punct — check whether printable characters contain no whitespace, digits or letters

    • If every character in text is printable but is not a letter, a digit or whitespace, returns TRUE; otherwise returns FALSE.
  • ctype_upper — check for uppercase letters

    • In the current locale, if every character in text is an uppercase letter, returns TRUE.
  • ctype_xdigit — check whether a string contains only hexadecimal characters

    • If every character in text is a hexadecimal character, that is, it can only contain decimal digits and the letters [A-Fa-f]. Otherwise, returns FALSE
  • filter_has_var — check whether a variable of the specified type exists

    • type One of INPUT_GET, INPUT_POST, INPUT_COOKIE, INPUT_SERVER, INPUT_ENV.
    • variable_name the name of the variable to check.
    • Returns TRUE on success, or FALSE on failure.
  • register_shutdown_function — register a function to be executed when PHP terminates

    • callback the shutdown callback to register
    • The shutdown callbacks are executed as part of the request, so you can output to them or read the output buffer in them.
    • parameter you can pass additional parameters to pass arguments to the shutdown function
    • register_shutdown_function() can be called multiple times; these registered callbacks will be called in the order they were registered. If you call exit() inside a registered method, then all processing is aborted and the other registered shutdown callbacks will not be called either.
    • If the callback passed in is not callable, an E_WARNING level error will be raised.