# Round up to nearest multiple of five in PHP

Viewed   77 times

I want a php function which returns 55 when calling it with 52.

I've tried the `round()` function:

``````echo round(94, -1); // 90
``````

It returns 90 but I want 95.

Thanks.

5

This can be accomplished in a number of ways, depending on your preferred rounding convention:

## 1. Round to the next multiple of 5, exclude the current number

Behaviour: 50 outputs 55, 52 outputs 55

``````function roundUpToAny(\$n,\$x=5) {
return round((\$n+\$x/2)/\$x)*\$x;
}
``````

## 2. Round to the nearest multiple of 5, include the current number

Behaviour: 50 outputs 50, 52 outputs 55, 50.25 outputs 50

``````function roundUpToAny(\$n,\$x=5) {
return (round(\$n)%\$x === 0) ? round(\$n) : round((\$n+\$x/2)/\$x)*\$x;
}
``````

## 3. Round up to an integer, then to the nearest multiple of 5

Behaviour: 50 outputs 50, 52 outputs 55, 50.25 outputs 55

``````function roundUpToAny(\$n,\$x=5) {
return (ceil(\$n)%\$x === 0) ? ceil(\$n) : round((\$n+\$x/2)/\$x)*\$x;
}
``````
Wednesday, December 7, 2022
3

# TL;DR:

The PHP native function bcdiv seems to do precisely what is required, and properly.

To simply "truncate" a number, `bcdiv(\$var, 1, 2);` where 2 is the number of decimals to preserve (and 1 is the denomenator - dividing the number by 1 allows you to simply truncate the original number to the desired decimal places)

This turns out to be more elusive than one might think.

After this answer was (incorrectly) upvoted quite a bit, it has come to my attention that even sprintf will round.

Rather than delete this answer, I'm turning it into a more robust explanation / discussion of each proposed solution.

number_format - Incorrect. (rounds)
Try using number format:

``````\$var = number_format(\$var, 2, '.', '');  // Last two parameters are optional
echo \$var;
// Outputs 2.50
``````

If you want it to be a number, then simply type-cast to a float:

``````\$var = (float)number_format(\$var, 2, '.', '');
``````

Note: as has been pointed out in the comments, this does in fact round the number.

sprintf - incorrect. (sprintf also rounds)
If not rounding the number is important, then per the answer below, use sprintf:

``````\$var = sprintf("%01.2f", \$var);
``````

floor - not quite! (floor rounds negative numbers)

floor, with some math, will come close to doing what you want:

``````floor(2.56789 * 100) / 100; // 2.56
``````

Where 100 represents the precision you want. If you wanted it to three digits, then:

``````floor(2.56789 * 1000) / 1000; // 2.567
``````

However, this has a problem with negative numbers. Negative numbers still get rounded, rather than truncated:

``````floor(-2.56789 * 100) / 100; // -2.57
``````

# "Old" Correct answer: function utilizing floor

So a fully robust solution requires a function:

``````function truncate_number( \$number, \$precision = 2) {
// Zero causes issues, and no need to truncate
if ( 0 == (int)\$number ) {
return \$number;
}
// Are we negative?
\$negative = \$number / abs(\$number);
// Cast the number to a positive to solve rounding
\$number = abs(\$number);
// Calculate precision number for dividing / multiplying
\$precision = pow(10, \$precision);
// Run the math, re-applying the negative value to ensure returns correctly negative / positive
return floor( \$number * \$precision ) / \$precision * \$negative;
}
``````

Results from the above function:

``````echo truncate_number(2.56789, 1); // 2.5
echo truncate_number(2.56789);    // 2.56
echo truncate_number(2.56789, 3); // 2.567

echo truncate_number(-2.56789, 1); // -2.5
echo truncate_number(-2.56789);    // -2.56
echo truncate_number(-2.56789, 3); // -2.567
``````

Use the PHP native function bcdiv

``````echo bcdiv(2.56789, 1, 1);  // 2.5
echo bcdiv(2.56789, 1, 2);  // 2.56
echo bcdiv(2.56789, 1, 3);  // 2.567
echo bcdiv(-2.56789, 1, 1); // -2.5
echo bcdiv(-2.56789, 1, 2); // -2.56
echo bcdiv(-2.56789, 1, 3); // -2.567
``````
Tuesday, September 6, 2022
3

Insert Mutliple values in a single Field:

For inserting the flat no's in a single field, use `implode` and the values separated by comma.

``````\$r=implode(",",\$available);
\$insert=mysql_query("insert into flatdetails(available) value ('\$r')")
``````

For retrieve the values from database use, `explode` to separate out all the values..

For Retrieve the values using \$i=explode(",",\$r);

``````<?php
\$select_tbl=mysql_query("select * from flatdetails",\$conn);
while(\$fetch=mysql_fetch_object(\$select_tbl))
{
\$r=\$fetch->available;

\$i=explode(",",\$r);

echo \$i[0]."</br>";
}
?>
``````
Wednesday, September 14, 2022
2

Cracks knuckles

Technically the syntax is "correct" (it won't generate a fatal error) but the semantics of PHP render it effectively meaningless in its current form. Let's look at a few things first, namely how PHP handles the assignment of named functions to variables:

``````php > echo shell_exec("php -v");
PHP 5.4.16 (cli) (built: Oct 30 2018 19:30:51)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

php > function speak(\$arg) {echo "{\$arg}n";}
php > function give(\$arg) {return \$arg;}
php > \$speak = speak(4);
4
php > \$give = give(4);
php > var_dump(\$speak);
NULL
php > var_dump(\$give);
int(4)
``````

The function itself is executed upon assignment and its return value (NULL or otherwise) is assigned to the variable. Since we're only assigning the return value of a function's execution, trying to use this variable as a function name has no use:

``````php > \$speak(4);
php > \$give(4);
php >
``````

Let's contrast this to the assignment of an anonymous function (a.k.a. a 'Closure'):

``````php > \$min = 1; \$max = 6;
php > \$checkName = function (\$value) use (\$min, \$max) {
php {   echo "value: {\$value}n";
php {   echo "min: {\$min}n";
php {   echo "max: {\$max}n";
php { };
php > var_dump(\$checkName);
object(Closure)#1 (2) {
["static"]=>
array(2) {
["min"]=>
int(1)
["max"]=>
int(6)
}
["parameter"]=>
array(1) {
["\$value"]=>
string(10) "<required>"
}
}
``````

Unlike some other languages, a closure is represented in PHP by an actual Object. Variables inside the 'use' clause are imported at the time the Closure was created; function parameters (i.e. \$value) have their values captured when the Closure is called (hence why we see it noted as a required parameter and not a static value). The semantics of references within Closures aren't worth considering right now but if you want further reading, goat's answer to this question is a great start.

The major takeaway here is that the Closure's assignment to \$checkName did not execute the Closure itself. Instead, \$checkName becomes a sort of "alias" we can use to reference this function by name:

``````php > \$checkName("hello ");
value: hello
min: 1
max: 6
php >
``````

Given how loose PHP is about the number of function parameters passed, a zero-parameter execution returns expected results:

``````php > \$checkName();
value:
min: 1
max: 6
php >
``````

Now let's take it another level deeper and define a function within a function:

``````php > function myOuterFunc(\$arg) {
php {   function myInnerFunc(\$arg){
php {     echo "{\$arg}n";
php {   }
php { }
php > \$myVal = myOuterFunc("Hello ");
php > var_dump(\$myVal);
NULL
php >
``````

By now this result should make sense. Functions do not execute unless explicitly called; just because we call myOuterFunc doesn't mean we execute any function code defined inside of it. That's not to say that we couldn't:

``````php > function myOuterFunc(\$arg) {
php {   function myInnerFunc(\$arg){
php {     echo "{\$arg}n";
php {   }
php {   myInnerFunc(\$arg);
php { }
php > \$myVal = myOuterFunc("Hello ");
Hello
php > var_dump(\$myVal);
NULL
php >
``````

Which brings us back around to what is essentially your question: what about a named function inside of a Closure? Given what we've now discovered about function execution, we can generate a series of very predictable examples:

``````\$min = 1; \$max = 6;
\$checkName = function (\$value) use (\$min, \$max) {
function question(){echo "How are youn";}
echo "value: {\$value}n";
echo "min: {\$min}n";
echo "max: {\$max}n";
};
php > \$checkName("Hello ");
value: Hello
min: 1
max: 6
php >
``````

As expected, the named function's code inside the Closure is not executed because we have not explicitly called it.

``````php > \$min = 1; \$max = 6;
php > \$checkName = function (\$value) use (\$min, \$max) {
php {   function question(){echo "How are youn";}
php {   echo "value: {\$value}n";
php {   echo "min: {\$min}n";
php {   echo "max: {\$max}n";
php {   question();
php { };
php > \$checkName("Hello ");
value: Hello
min: 1
max: 6
How are you
php >
``````

Explicitly calling the inner function works just fine, provided we define that function before we call it:

``````php > \$min = 1; \$max = 6;
php > \$checkName = function (\$value) use (\$min, \$max) {
php {   echo "value: {\$value}n";
php {   echo "min: {\$min}n";
php {   echo "max: {\$max}n";
php {   question();
php {   function question(){echo "How are youn";}
php { };
php > \$checkName("Hello ");
value: Hello
min: 1
max: 6
php >

php > \$min = 1; \$max = 6;
php > \$checkName = function (\$value) use (\$min, \$max) {
php {   echo "value: {\$value}n";
php {   echo "min: {\$min}n";
php {   echo "max: {\$max}n";
php {   function question(){echo "How are youn";}
php {   question();
php { };
php > \$checkName("Hello ");
value: Hello
min: 1
max: 6
How are you
php >
``````

So to the point of your questions then:

1. Yes it's legal and what you're attempting is possible but semantically meaningless in its current form.

2. Any named functions inside the Closure definitely do not reside in the global namespace, they are within the scope of their defining Closure. FWIW, the term "members" typically refers to class variables (usually called "properties" in PHP). While Closures are an Object and let you duplicate the functionality of instance variables found within classes, they should not be confused or construed with classes in any way.

3/4) Not how you're trying to use it, no. Nothing outside of the Closure has any concept of the functions inside; if in calling your Closure code, said code performs operations using the inner functions, then they will see the light of day. But there is no immediate way to reference those inner functions as if they were defined outside of the Closure's scope.

In other words, the only way you'll get those inner functions to execute is if a) that code is specifically executed by the Closure's code and b) you execute said Closure code.

Hope this helps.

Tuesday, December 20, 2022
2

Use `round()` instead of `ceil()`.

``````5 * round(\$n / 5);
``````

`ceil()` rounds a floating point number up to its next integer in sequence. `round()` will round to the nearest integer using standard rounding rules.

Sunday, December 11, 2022