How to convert string to integer in php

王林
Release: 2023-03-03 20:46:01
Original
3992 people have browsed it

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])
Copy after login

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 是整型
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!