In PHP,intval()
Functions are used to convert a string into an integer. Its syntax is as follows:
intval(string $value, int $base = 10): int
Parameter description:
-
$value
: The string to be converted. -
$base
(Optional): The number of binary numbers, default is 10. If another binary number is specified, the string is interpreted as a number of the specified number of digits. For example, interpret a string as a binary number using binary (2), interpret a string as an octal number using octal (8), and interpret a string as an hexadecimal number using hexadecimal (16).
Return value:
- The converted integer value.
- If the conversion fails or the string cannot be converted to an integer, return
0
。
Here are some examples that illustrateintval()
Function usage:
Example 1:Converts a string to a decimal integer.
$numberString = "12345"; $result = intval($numberString); echo $result; // Output:12345
Example 2:Converts a string to an integer in other binary.
$numberString = "10101"; $result = intval($numberString, 2); // Binary numberecho $result; // Output:41(Binary number10101Convert to decimal number to41)
Example 3:Converts a string with leading zeros.
$numberString = "000123"; $result = intval($numberString); echo $result; // Output:123(Leading zeros will be ignored)
Example 4:Converts a string with non-numeric characters.
$numberString = "123abc"; $result = intval($numberString); echo $result; // Output:123(Non-numeric characters will be ignored)
Summarize:intval()
Functions are used to convert strings into integers, can specify a binary number, and can handle leading zeros and non-numeric characters.
When usingintval()
When function, if the passed string is empty, it will return0
. Even if the passed string is an empty string (""),intval()
It will also be interpreted as a number0
and return. This is becauseintval()
The function is to convert a string into an integer, and an empty string is mathematically interpreted as0
。
This is the end of this article about the example usage of php intval function. For more related php intval function content, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!