How to convert string to int type in php

王林
Release: 2023-03-05 13:34:01
Original
3548 people have browsed it

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")].

How to convert string to int type in php

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

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

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!

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