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

How to convert value to integer in php

青灯夜游
Release: 2023-03-15 10:02:01
Original
3678 people have browsed it

Method: 1. Add "(int)" or "(integer)" before the value, for example "3.14(int)"; 2. Use the intval() function, the syntax is "intval(value)" ;3. Use the ceil() function, the syntax is "ceil(value)"; 4. Use the floor function, the syntax is "floor(value)".

How to convert value to integer in php

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

php transfers the value For an integer

1, add "(int)" or "(integer)" before the value

<?php
$str = &#39;123.456abc&#39;;
$int = (int)$str;
echo $int."<br>";

$float = 3.14;
$int = (integer)$float;
echo $int."<br>";
?>
Copy after login

How to convert value to integer in php

2. Use the intval() function

<?php
$str = &#39;123.456abc&#39;;
$int = intval($str);
echo $int."<br>";

$float = 5.64;
$int = intval($float);
echo $int."<br>";

$bool = true;
$int = intval($bool);
echo $int."<br>";
?>
Copy after login

How to convert value to integer in php

##3. Use the ceil() function

ceil() function is used to round up. If there is a decimal, add 1 to the integer part

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

How to convert value to integer in php

4. Use the floor function

floor function is used to round down

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

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!

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