A smarter approach would be to write a function that uses many conditional statements to return incremental values ​​based on different numerical ranges
P粉311464935
P粉311464935 2023-08-17 23:18:09
0
1
323
<p>I'm trying to write a function that returns a specific value if the input is within a certain range of numbers. </p> <p>Normally, I would write a series of if/else if statements to accomplish this task. However, I'm dealing with a large number of ranges, so writing out 30+ if/else if statements seems inefficient. </p> <p>Suppose I have a minimum and maximum number for an overall range, and I want to split it into smaller ranges. For each range I want to return some values. Ideally, the return value for each range would be decremented by some fixed number. </p> <p>To provide context, I'm trying to calculate a value based on mileage using something like this. For example, if the mileage is between 101-120 miles, the rate is equal to 80% of the mileage. The percentage decreases as mileage increases. Moreover, the value of the percentage decrease can be a fixed number (such as <code>-0.05</code>)</p> <pre class="brush:php;toolbar:false;">... else if (m >= 101 && m <= 120) return 0.80 else if (m >= 121 && m <= 140) return 0.75 ...</pre> <p>Since there is a pattern in the return value for each range, I think it can be done in a better way than writing a bunch of if/else statements, but I'm not sure exactly how. Any ideas? </p>
P粉311464935
P粉311464935

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!