round a number to the nearest integer in php

To round a number to the nearest integer in PHP, you can use the round() function. This function rounds a number to the nearest integer, using the standard rounding rules (if the number is halfway between two integers, it rounds to the nearest even integer).

Here is an example usage of the round() function:

main.php
$num = 3.6;
$rounded_num = round($num);
echo $rounded_num; // Output: 4
72 chars
4 lines

In this example, we first define a variable $num which contains the number we want to round. We then use the round() function to round this number to the nearest integer, and store the result in a new variable $rounded_num.

Finally, we use the echo statement to output the rounded number to the screen. The output in this case will be 4, since 3.6 is closer to 4 than it is to 3.

gistlibby LogSnag