Home > Article > Backend Development > How to determine whether a string is an integer in php
Two methods: 1. Use intval() to convert the string into an integer value, and then use the "==" operator to determine whether the integer value is equal to the original string. The syntax "intval($str)= =$str", if equal, it is an integer. 2. Use is_numeric() and strpos() to determine whether the string is a numeric string without a decimal point. The syntax is "!is_numeric($str)||strpos($str,".")!==false" and return false is an integer, and vice versa is not.
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
php determines the string Two methods to determine whether it is an integer
Method 1: Use the intval() function and the "==" operator to determine
Use the intval() function to convert the string into an integer value
Use the "==" operator to determine whether the integer value is equal to the original string
<?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("a678"); f("678"); f("3.14"); ?>
Method 2: Use is_numeric() function and strpos() function to judge
Use The is_numeric() function determines whether it is a number or a numeric string (integer and floating point number)
Use strpos() to determine whether the number contains a decimal point--exclude floating point numbers
You only need to determine whether the string is a numeric string that does not contain a decimal point
<?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"); ?>
Instructions:
intval() function
intval() function is used 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.
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 )
Parameter description:
$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)
Parameter description:
$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 a string is an integer in php. For more information, please follow other related articles on the PHP Chinese website!