search
HomeBackend DevelopmentPHP ProblemHow to convert value to integer in php

3 conversion methods: 1. Use the intval() function to get the integer value of the variable, the syntax "intval($var)"; 2. Use the settype() function to set the variable value to an integer type, the syntax is "settype($var,'integer')"; 3. Add the target type "(int)" or "(integer)" enclosed in parentheses before the data, for example "(int)123.456".

How to convert value to integer in php

The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer

Convert the value in php to Integer type (int) method:

Method 1: Use the intval() function

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

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.

Syntax:

int intval ( mixed $var [, int $base = 10 ] )

Parameter description:

  • $var: The quantity value to be converted to integer.

  • #$base: The base used for conversion.

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

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

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

  • Decimal will be used.

Return value: The integer value of var is returned on success, and 0 is returned on failure. An empty array returns 0, a non-empty array returns 1.

Example: Convert the value to int type

<?php 
header("Content-type:text/html;charset=utf-8");  
$num1=3.1415926;   
$num2=intval($num1); 
echo "浮点型转为int型:";
var_dump($num1); 
var_dump($num2); 

$str1="123d";   
$str2=intval($str1); 
echo "字符串型转为int型:";
var_dump($str1); 
var_dump($str2); 
?>

How to convert value to integer in php

Method 2: Use the settype() function

settype() function: used to set the type of variables.

The type that can be set is:

  • "boolean" (or "bool", starting from PHP 4.2.0)

  • "integer" (or "int" since PHP 4.2.0)

  • "float" (only available since PHP 4.2.0, for older versions "double" used in is now deprecated)

  • "string"

  • "array"

  • "object"

  • "null" (from PHP 4.2.0)

Note: The settype() function will change the variable type itself.

Example: Convert floating point type to int type

<?php
header("Content-type:text/html;charset=utf-8");
$num=2.24;
$str="52.36";
echo &#39;原 $num 和 $str:<br>&#39;;
var_dump($num); 
var_dump($str); 

settype($num, &#39;integer&#39;);
settype($str, &#39;integer&#39;);
echo &#39;<br>现 $num 和 $str:<br>&#39;;
var_dump($num); 
var_dump($str); 
?>

How to convert value to integer in php

Method 3: Add the target type enclosed in parentheses before the data "(int)" or "(integer)"

  • (int), (integer): Convert to integer type;

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

How to convert value to integer in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to convert value 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function