Home > Article > Backend Development > How to determine whether it is an integer string in php
Two judgment methods: 1. Use is_numeric() and strpos() to judge whether the string is a numeric string that does not contain a decimal point. The syntax is "!is_numeric($str)||strpos($str," .")!==false", if false is returned, it is an integer string, and vice versa. 2. Use intval() to convert the string into an integer value, and use the "==" operator to determine whether the integer value is equal to the original string. The syntax is "intval($str)==$str".
The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer
php determines whether a string For an integer string
Method 1: Use the is_numeric() function and strpos() function to judge
You only need to judge whether the string is A numeric string without a decimal point is enough
Example: Check whether the strings "a678", "3.14", and "67854" are integer strings.
<?php header("Content-type:text/html;charset=utf-8"); function f($str){ if(!is_numeric($str)||strpos($str,".")!==false){ echo "$str 字符串不是整数字符串<br><br>"; }else{ echo "$str 字符串是整数字符串<br><br>"; } } f("a678"); f("3.14"); f("67854"); ?>
Analysis:
is_numeric() function can determine whether it is a number or a numeric string, but it cannot check out the character Whether the string is an integer, that is, the string may be an integer or a floating point number;
In this case, you need to use strpos() to judge, and use this function to check whether the numeric string contains a decimal point , which excludes floating point numbers.
Description:
intval() function
intval() function To get the integer value of a variable.
intval() function returns the integer value of variable var by using the specified base conversion (default is decimal). intval() cannot be used with object, otherwise an E_NOTICE error will be generated and 1 will be returned.
int intval ( mixed $var [, int $base = 10 ] )Parameter description:
$var: The quantity value to be converted to integer.
#$base: The base used for conversion.
If base is 0, the base used is determined by detecting the format of var:
If the string includes "0x" ( or "0X"), use hexadecimal (hex); otherwise,
If the string starts with "0", use octal (octal); otherwise,
Decimal will be used.
Return value
Returns the integer value of var on success, and returns 0 on failure. An empty array returns 0, a non-empty array returns 1.
The maximum value depends on the operating system. The maximum signed integer range on 32-bit systems is -2147483648 to 2147483647. For example, on such a system, intval('1000000000000') would return 2147483647. On a 64-bit system, the maximum signed integer value is 9223372036854775807.
String may return 0, although it depends on the leftmost character of the string.
Method 2: Use the intval() function and the "==" operator to make a judgment
You only need to convert the string is an integer, and then determine whether the integer is equal to the previous string.
Example:
<?php header("Content-type:text/html;charset=utf-8"); function f($str){ $result = intval($str); if($result==$str){ echo "$str 字符串是整数字符串<br><br>"; } else{ echo "$str 字符串不是整数字符串<br><br>" ; } } f("678"); f("678d"); f("a678"); f("3.14"); ?>
Analysis:
Use the intval() function to convert a string into an integer value
Use the "==" operator to determine whether the integer value is equal to the original string
Instructions :
is_numeric() function
is_numeric() function is used to detect whether a variable is a number or a numeric string.
bool is_numeric ( mixed $var )
$var: Variable to be detected.
Return value
If the specified variable is a number or numeric string, it returns TRUE, otherwise it returns FALSE. Note that floating point type returns 1 , that is, TRUE.
strpos() function
strpos() function finds the first occurrence of a string in another string (size sensitive Write).
strpos($string,$find,$start)
$string : Required. Specifies the string to be searched for.
#$find: required. Specifies the characters to search for.
$start: Optional. Specifies the location from which to start the search.
Return value:
Returns the position of the first occurrence of a string in another string, or returns if the string is not found FALSE. Note: The string position starts from 0, not from 1.
Recommended study: "PHP Video Tutorial"
The above is the detailed content of How to determine whether it is an integer string in php. For more information, please follow other related articles on the PHP Chinese website!