Home > Article > Backend Development > How to convert string to int type in php
How to convert a string to int type in php: You can use the built-in function intval() to achieve this. The intval() function is used to obtain the integer value of a variable. If the execution is successful, it returns an integer value. If the execution fails, it returns 0, for example: [intval("1")].
Function introduction:
intval() function is used to obtain the integer value of a variable.
(Recommended tutorial: php video tutorial)
intval() function returns the integer value of the 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:
int intval ( mixed $var [, int $base = 10 ] )
Parameters:
$var: The quantity value to be converted to integer.
#$base: The base used for conversion.
(Related recommendations: php training)
Return value:
Returns the integer value of var when successful, and when failed Return 0. An empty array returns 0, a non-empty array returns 1.
Code example:
<?php $foo = "1"; // $foo 是字符串类型 $bar = intval($foo); // $bar 是整型 ?>
The above is the detailed content of How to convert string to int type in php. For more information, please follow other related articles on the PHP Chinese website!