Home >Backend Development >PHP Problem >How to convert string to integer in php

How to convert string to integer in php

王林
王林Original
2020-08-05 15:58:094100browse

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.

How to convert string to integer in php

#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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn