I want to get the length of integer values for validation in PHP. Example: Mobile numbers should be only 10 integer values. It should not be more than 10 or less than 10 and also it should not be included of alphabetic characters.
How can I validate this?
filter_var($int, FILTER_VALIDATE_INT)) { echo "Only integer values are required!"; exit(); } else { // Convert the integer to array $int_array = array_map('intval', str_split($int)); //get the lenght of the array $int_lenght = count($int_array); } //Check to make sure the lenght of the int does not exceed or less ...
Using Continuous Multiplication We can multiply a number 1 by 10 until it becomes greater than the number n. Each time we multiply by 10, we increment a variable count by 1. The final value of the count gives the length of the integer n.
PHP Integers An integer data type is a non-decimal number between -2147483648 and 2147483647 in 32 bit systems, and between -9223372036854775808 and 9223372036854775807 in 64 bit systems. A value greater (or lower) than this, will be stored as float, because it exceeds the limit of an integer.
The is_int() function checks whether a variable is of type integer or not. This function returns true (1) if the variable is of type integer, otherwise it returns false.
$num_length = strlen((string)$num); if($num_length == 10) { // Pass } else { // Fail }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With