How to use php intval()

青灯夜游
Release: 2023-03-12 11:50:02
Original
2793 people have browsed it

In PHP, the intval() function can obtain the integer value of a variable and is often used for data type conversion to convert string type variables into integer types; the specific usage syntax is "intval($ var[,$base])".

How to use php intval()

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

intval() function is used to obtain the integer of the variable value. Syntax format:

int intval ( mixed $var [, int $base = 10 ] )
Copy after login
  • $var: The quantity value to be converted to integer.

  • #$base: The base used for conversion.

    If base is 0, determine the base used by detecting the format of var:

    • If the string includes "0x" (or "0X") Prefix, use hexadecimal (hex); otherwise,

    • If the string starts with "0", use octal (octal); otherwise,

    • Decimal will be used.

The intval() function is often used for data type conversion to convert string type variables into integer types.

intval() function returns the integer value of 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.

Example: Convert a string to an integer

<?php
header("Content-type:text/html;charset=utf-8"); 
$str = &#39;123.456abc&#39;;
$int = intval($str);
echo &#39;变量 $int 的类型为:&#39;.gettype($int).&#39;<br>&#39;;
var_dump($int);
?>
Copy after login

Output result:

How to use php intval()

Recommended Study: "PHP Video Tutorial"

The above is the detailed content of How to use php intval(). 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