Home >Backend Development >PHP Problem >How to convert string to integer in php
php method to convert a string into an integer: You can use the intval() function to achieve this. The intval() function returns the integer value of the variable by using the specified base conversion. The intval() function cannot be used on object, otherwise an error will occur.
#intval() function is used to get the integer value of a variable.
(Recommended tutorial: php graphic tutorial)
intval() function returns the integer of variable var by using the specified base conversion (default is decimal) numerical value. intval() cannot be used with object, otherwise an E_NOTICE error will be generated and 1 will be returned.
Syntax:
int intval(mixed $var[, int $base = 10])
Parameter introduction:
$var: The quantity value to be converted to integer.
#$base: The base used for conversion.
(Video tutorial recommendation: php video tutorial)
Return value:
Returns the integer value of var when successful, Returns 0 on failure. An empty array returns 0, a non-empty array returns 1.
Code implementation:
<?php $foo = "1"; // $foo 是字符串类型 $bar = intval($foo); // $bar 是整型 ?>
The above is the detailed content of How to convert string to integer in php. For more information, please follow other related articles on the PHP Chinese website!