I have a variable $user_id (number value)
Want to check if $user_id falls in between the range of 1 - 1000 or 1001 - 2000 or 2001 - 3000 .... 99001 - 100000
Is there a way to do this without writing 100 switch or if statements in PHP?
When it finds the match, execute a code.
I know while and for loops are required for this. But not able to code it properly.
This the simplest way:
$check = 0;
$nextCheck = $check+1001;
while ($check < 100001) {
If ($user_id > $check && $user_id < $nextCheck) {
// Code ...
break;
} else {
$check+=1000;
$nextCheck+=1000;
}
}
You can just divide the number by 1000 since you have a 1000 interval range in a consecutive way.
The quotient * 1000 + 1 is your start value for the interval with an exceptional corner case of a number divisible by 1000, which would just be the border end for an interval.
<?php
$tests = [1,999,1000,50001,100000,2999];
foreach($tests as $test_case){
$quotient = intval($test_case / 1000);
if($test_case % 1000 === 0){
$start = $test_case - 1000 + 1;
echo "$test_case : Range: ($start - $test_case)",PHP_EOL;
}else{
$start = $quotient * 1000 + 1;
$end = ($quotient + 1) * 1000;
echo "$test_case : Range: ($start - $end)",PHP_EOL;
}
}
Output:
1 : Range: (1 - 1000)
999 : Range: (1 - 1000)
1000 : Range: (1 - 1000)
50001 : Range: (50001 - 51000)
100000 : Range: (99001 - 100000)
2999 : Range: (2001 - 3000)
Demo: https://3v4l.org/apbqV
Do not generate an array. Do not use a loop. Use math to determine the upper limit of the range, then subtract 999 from that number -- done.
*my snippet assumes we are only dealing with positive values between 1 and 100000.
Code: (Demo)
$tests = [1, 999, 1000, 50001, 100000, 2999];
foreach ($tests as $test) {
$upper = intval(($test - 1) / 1000) * 1000 + 1000;
echo "$test is between " . ($upper - 999) . " and $upper\n";
}
Output:
1 is between 1 and 1000
999 is between 1 and 1000
1000 is between 1 and 1000
50001 is between 50001 and 51000
100000 is between 99001 and 100000
2999 is between 2001 and 3000
Formula Breakdown:
intval( #3) remove decimals from difference
($test - 1) #1) subtract one
/ 1000 #2) divide by 1000
)
* 1000 #4) multiply integer by 1000
+ 1000 #5) add 1000
Related
I have a question and I don't know what is the correct term to use in search!
what PHP function should I use to print the number like this
Original number > Expected result
15001 > 15000
16300 > 16000
22700 > 22000
I mean to remove all numbers after the thousands
any help ?
Just some math:
Divide the $number by 1000, round fractions down with floor or ceil, then multiply it by 1000.
$number = 22700;
// if ($number < 1000) { /* don't apply this */ }
echo floor($number / 1000) * 1000; // 22000
// or
echo ceil($number / 1000) * 1000; // 23000
One way to rome:
foreach([1,12,123,1234,12345,123456,1234567] as $num){
print $num. " > ". (substr($num,-1*strlen($num),-3)."000") . '<br>';
}
//result:
//1 > 000
//12 > 000
//123 > 000
//1234 > 1000
//12345 > 12000
//123456 > 123000
//1234567 > 1234000
No rounding here. Just replacing.
i want to round up a number like this
1439 to 1400
1399 to 1350
What are the nearest way to do this in php?
Given the new examples...
Looks like you want to use PHP floor instead and apply the 50 yourself
50 * floor($number / 50)
OLD ANSWER
Going from your examples, rather than the question title..
Try the PHP round function.
In your case:
round($number, -2);
The second param is the number of decimal figures to round to, negative values go to the left of the 'ones' digit instead.
There is also a third parameter for some more subtle variations.
$rounded_n1 = round($n1 / 50, 0) * 50;
You can do something like that (only to round down) :
$n1 = 1439;
$n2 = 1399;
$round1 = $n1 - $n1 % 50; // round1 = 1439 - 39 = 1400
$round2 = $n2 - $n2 % 50; // round2 = 1399 - 49 = 1350
To round up, you can do this :
$n1 = 1439;
$n2 = 1399;
$round1 = $n1 + (50 - $n1 % 50); // round1 = 1439 + (50 - 39) = 1450
$round2 = $n2 + (50 - $n2 % 50); // round2 = 1399 + (50 - 49) = 1400
You can do it like:
Divide it by 100.
Truncate.
Multiply by 100.
This is the best thing I could come up with.
$num = 1401;
$num /= 100;
$num = round($num);
$num *= 100;
Use ceil to always round up
$round1 = ceil($n1/ 50) * 50
You should try:
function specialRoundUp($val) {
return 100 * round($val / 100);
}
Here I have total sales worth RM 3500.
I need to give points to every 1000 in that 3500.The point value is 0.1 and it should increase in same value for every subsequent increment.
So, In the example above, in RM 3500. I have 3 x 1000.
The first 1000 get 0.1 point. The next 1000 gets 0.2 and the third gets 0.3 and so on. Anything below 1000 will not make any changes.How do I write this as a function in PHP?
I can only think of if else statement but this is not efficient.
//default
$increament = 0.1;
//calculate increament
if($new_sales == 1000)
{
$increment +=0.1;
}...after this I don't know how to write for subsequent 1000
Try like this:
$increment = (floor(3500/1000)/10);
or
$increment = (floor(3500/1000)*0.1);
from your code:
$increment = (floor($new_sales/1000)*$increament);
You can take the sales total and divide by 1000, round down and multiply by 0.1.
$increment = floor($new_sales / 1000) * 0.01;
I hope i understood it correctly:
<?php
$points = 0;
$startingPoint = 0.1;
if($new_sales >= 1000)
{
$increment = floor($new_sales/1000);
// 3500 / 1000 --> 3
for($i = 0;i <= $increment;$i++)
{
$points += $startingPoint * $i;
// ex. points += 0.1 * 1
// points += 0.1 * 2
}
}
?>
In the PHP function range there are a start point, a end point and a step point.
is it possible to create an array with numbers, in which some values should not exist?
$hundred_tens = range(120, 190, 10);
I need the numbers 220,230,...290 ... 920,930..990, but not 200,210 ...900,910.
My solution:
$hundreds = range(100, 800, 100);
foreach ($hundred_tens as $value) {
$add_numbers[] = $value + $hundreds[0];
$add_numbers[] = $value + $hundreds[1];
$add_numbers[] = $value + $hundreds[2];
$add_numbers[] = $value + $hundreds[3];
$add_numbers[] = $value + $hundreds[4];
$add_numbers[] = $value + $hundreds[5];
$add_numbers[] = $value + $hundreds[6];
$add_numbers[] = $value + $hundreds[7];
}
$all_hundred_tens = array_merge($hundred_tens, $add_numbers);
I have add in a foreach every array value $hundreds to in a new array and merge this array with $hundred_tens.
You can either loop through your entire range and exclude the unnecessary numbers
$hundred_tens = array();
foreach (range(100, 800, 10) as $number) {
if ($number % 100 !== 0 && $number % 100 !== 10) {
$hundred_tens[] = $number;
}
}
or you can merge smaller ranges:
$hundred_tens = array_merge(range(120, 190, 10), range(220, 890, 10), range(920, 990, 10));
Which one is better depends on how many exclusions you have, and if they are together in the list.
Ref my comment below about for ( being faster, here's an example (only one line is different)
$hundred_tens = array();
for ($number = 100; $number <= 1000; $number += 10) {
if ($number % 100 !== 0 && $number % 100 !== 10) {
$hundred_tens[] = $number;
}
}
Basically, you create a for-loop starting from 220 till 910 in steps of 10. In the loop you check whether the number is a hundred or a hundred plus ten. If it is not, then add the number to the array.
To check whether a number is divisable by 100, you could use the module operator %.
So, a % 100 == 0 will result in true, if a is divisable by 100 and false otherwise. For hundreds plus ten, you could do: (a - 10) % 100 == 0.
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 3
[6] => 1
)
I was wondering how one would go about working out the average percentage difference between the current value in an array and the next value. If the next value were a larger one, it would perform like so. (ie keys [0]-[1] 1/2 * 100 = 50). If it were a smaller value it would perform like so. (ie keys [4]-[5] = 3/5 * 100 = -60).
The following will represent what I am aiming to do with these percentage calculations.
1/2 * 100
2/3 * 100
3/4 * 100
4/5 * 100
3/5 * 100 (negative)
1/3 * 100 (negative)
Total : total/count
This will iterate through the list and then work out the average from the count. I have looked into splitting arrays but don't see how else I could do this.
$count = count($num);
foreach ($num as $value) {
array_chunk($num, 1);
if($value<$value){
$total1 = $total1 + ($value/$value)*100;
}
if($value>$value){
$total2 = $total2 + ($value/$value)*100;
}
}
$average = (($total1-$total2)/$count);
print($average);
I understand the above code is incorrect, but I hope it reveals where I am getting at with this.
Any help would be greatly appreciated.
You don't want to use foreach as you'll always be needing two array elements. Note that this snippet does not protect you from 0 values. These will make your script fail.
$num = array(1, 2, 3, 4, 5, 3, 1);
$total = 0;
// The number of percent changes is one less than
// the size of your array.
$count = count($num) - 1;
// Array indexes start at 0
for ($i = 0; $i < $count; $i++) {
// The current number is $num[$i], and the
// next is $num[$i + 1]; knowing that it's
// pretty easy to compare them.
if ($num[$i] < $num[$i + 1]) {
$total += (100 * $num[$i] / $num[$i + 1]);
}
else {
$total += (-100 * $num[$i + 1] / $num[$i]);
};
};
echo ($total / $count);