Home > Article > Backend Development > How to convert string to number in php
php method to convert string to number
intval() function definition and usage
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.
Syntax
intval ( string [, base = 10 ] )
Parameters
string: The quantity value to be converted to integer.
base: The base used for conversion.
If base is 0, determine the base used by detecting the format of var:
If the string includes the prefix of "0x" (or "0X"), use hexadecimal (hex); otherwise,
if the string starts with "0", use octal; otherwise,
will use decimal.
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.
intval uses
<?php echo intval(42); // 42 echo intval(4.2); // 4 echo intval('42'); // 42 echo intval('+42'); // 42 echo intval('-42'); // -42 echo intval(042); // 34 echo intval('042'); // 42 echo intval(1e10); // 1410065408 echo intval('1e10'); // 1 echo intval(0x1A); // 26 echo intval(42000000); // 42000000 echo intval(420000000000000000000); // 0 echo intval('420000000000000000000'); // 2147483647 echo intval(42, 8); // 42 echo intval('42', 8); // 34 echo intval(array()); // 0 echo intval(array('foo', 'bar')); // 1
The above is the detailed content of How to convert string to number in php. For more information, please follow other related articles on the PHP Chinese website!