Functions for PHP Integers

The set of integers consists of all whole numbers, both positive and negative, and zero. In PHP, you can use integers for a variety of reasons, such as to save numeric values, as counters, unique identifiers, calculations and more. PHP also has several built-in functions that let you modify values, validate or convert them.

  1. Math Functions

    • For the most part, you use integers in calculations and comparisons. PHP has several built-in mathematical functions that perform trigonometric and algebraic calculations, return an integer's absolute value, multiply by a power and finds a square root. Its comparison functions include ones that let you find the maximum, minimum or average from a set of integers. PHP also has the "rand" function, which returns a random integer within a range that you specify.

    PHP "intval" Function

    • Integers consist solely of whole numbers without fractions. They take up less space in memory than floating-point numbers, which allow decimal places. If you convert a floating point value to an integer using the "intval" function, PHP rounds towards zero, not the nearest whole number. For example, the decimal number "4.9" becomes "4" as an integer. If you convert Boolean types to integers, the result is one or zero, depending whether the Boolean contained true or false. The result of a string conversion depends on the value of the initial string.

    Validation Function

    • PHP is a weakly typed programming languages, which means you are not required to explicitly declare your variables. In fact, you can create one variable, assign a string value to it, change that to an integer and then change it again to a Boolean. PHP provides data validation functions to check the type of a variable. The "is_int" function checks to see if a variable contains an integer value and returns "true" if it does, or "false" if it does not.

    Number Base

    • PHP lets you deal primarily with four number bases. In most cases, you use base-10, or decimal. You can create base-2 (binary), base-8 (octal) or base-16 (hexadecimal) numbers as well. To create octal integers, you must precede the number with a zero, and to create hexadecimal numbers, precede it with "0x." For example, the octal "050" value converts to 40 in decimal, while "0x65" in hexadecimal converts to 101 in decimal. PHP also has several built-in functions that converts from decimal to these three other bases and back.

Related Searches:

References

Comments

Related Ads

Featured