What is the difference between intval and floor functions in php
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.
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('42', 8); // 34 ?>
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
