PHP Integer Functions
An integer is a whole number. It does not have any decimal places. In PHP, you can create variables and assign integer values to them or use integers directly. PHP also has several built-in functions that take integers as arguments and modify them in some way to return new numbers. These kinds of functions include ones to check the data type, do calculations or convert numeric values.
-
PHP is_int Function
-
The "is_int" function takes one value as an argument and checks to see if it is an integer. You can pass either a literal value or a variable. If the value is an integer, the function evaluates to true, otherwise it returns false. This function helps you validate data, such as when you prompt a user to enter values for a calculator program, or you want him to select a record in a database based on a numeric identifier. The "is_integer" function is an alias of the "is_int" function.
PHP intval Function
-
The "intval" function takes an argument and returns its value as an integer. You can pass integers, floating point numbers or numbers in different base values to "intval." For example, 24 is already an integer, so "intval" returns 24. The hex value "0x3D" evaluates to 61. The floating point number 13.3 rounds down to 13. You can pass string values to the "intval" function, but in most cases, it returns zero.
-
Conversion Functions
-
Integers are numbers in base-10. That is, they have 10 digits, zero through nine. You can convert other bases to integers and integers to other bases using PHP conversion functions. To convert to integers from binary (base-2), octal (base-8) or hexadecimal (base-16), use the "decbin," "decoct" or "dechex" functions, respectively. To convert from these three bases to an integer, use the "bindec," "octdec" or "hexdec" functions. Each of these six functions takes one argument: the number to convert.
Regular Expressions
-
A regular expression is a series of characters that you match in a string. PHP's "preg_replace" function can check a string and extract all of the integers from it. The function takes a minimum of three arguments: the pattern, the replacement string, and the string to check. When extracting characters, you don't need to replace anything, so you can use a null value for this argument. For example, typing "preg_replace('/[^0-9]/', '', 'This string has 30 characters.');" returns the number 30.
-