[ create a new paste ] login | about

Link: http://codepad.org/eGBQp1Il    [ raw code | output | fork ]

PHP, pasted on Feb 19:
1
2
3
4
5
6
7
8
9
<?php
function limitRange($number, $min = 0, $max = 100) {
    return min(max($number,$min),$max);
}

for($i=-2;$i<=102;$i++) {
    echo 'Test for ' . $i . ': ' . limitRange($i) . PHP_EOL;
    if($i==5) { $i = 95; } // fastforward
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Test for -2: 0
Test for -1: 0
Test for 0: 0
Test for 1: 1
Test for 2: 2
Test for 3: 3
Test for 4: 4
Test for 5: 5
Test for 96: 96
Test for 97: 97
Test for 98: 98
Test for 99: 99
Test for 100: 100
Test for 101: 100
Test for 102: 100


Create a new paste based on this one


Comments: