Home  >  Article  >  Backend Development  >  What is the difference between intval and floor functions in php

What is the difference between intval and floor functions in php

青灯夜游
青灯夜游Original
2022-02-08 18:13:283148browse

Difference: 1. The intval() function directly rounds off the decimal part; while the floor() function rounds off the decimal part when the parameter is greater than 1. When the parameter is less than 1, it will Drop the decimal part and add 1 to the integer. 2. The intval() function can perform base conversion, but the floor function cannot.

What is the difference between intval and floor functions in php

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

In php, both intval and floor functions are available When it comes to rounding decimals, what's the difference between them? Find out below.

floor() function

floor() function rounds down to the nearest integer.

Syntax: floor(x)

Returns the next integer not greater than x, and rounds the decimal part of x. The type returned by floor() is still float because the range of float values ​​is usually larger than that of integer.

<?php
echo(floor(0.60));
echo(floor(0.40));
echo(floor(5));
echo(floor(5.1));
echo(floor(-5.1));
echo(floor(-5.9))
?>

Output:

0
0
5
5
-6
-6

intval() function

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

Syntax:

int intval(mixed var, int [base]);

Return value: integer

Function type: PHP system function

<?php 
echo intval(4.3); //4 
echo intval(4.6); // 4 
?>

Output:

4
4

intval() The function can return 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.

<?php
header("Content-type:text/html;charset=utf-8");
echo intval(42, 8)."<br>";                   // 42
echo intval(&#39;42&#39;, 8);                 // 34
?>

What is the difference between intval and floor functions in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of What is the difference between intval and floor functions 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